Skip to main content
GameDev.net gamedev.net
🔒 Locked

SHELL SCRIPTING: accessing individual lines

Started by matrisking Jun 2, 2006 at 3:15 PM 2 replies 800+ views
Original Post
matrisking
matrisking
Hi all, I'm writing a bash script and I have a file that is just a long list of file names, with one file on each line. I would like to select an individual line and return the filename on that line (and then proceed to delete the line from the file). I'm still learning about shell programming and I'm not sure if there is a simple command out there to do this or if its going to require some involved programming. If anyone knows of a simple, elegant way to do this, I'd really appreciate the help! Thanks a lot!
baldurk
baldurk
I am an awful, awful, awful scripter so please don't use this, but it works:

grep -n '.\?' $FILENAME | egrep '^'"$LINENO" | sed -e '{s%^[0-9]\+:%%}'

EDIT: there is also probably a very easy simple way to do it.
bytecoder
bytecoder
It'd probably be a lot easier to just use a programming language; this took me about a minute:
#!/usr/bin/pythonimport sysnames = open(sys.argv[1]).readlines()line = int(sys.argv[2])print names[line-1]del names[line-1]open(sys.argv[1], "w").write("".join(names))


edit:
Whoops. Darned python and its not-stripping-newline-off behavior :/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.