LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-05-2006, 02:30 AM   #1
pawarsac
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Rep: Reputation: 0
Shell programming


Hello
I want to read one file line by line by my shell script
(Just like reading file in C by fopen and then getch/gets).
After reading it i want to delete that line.
How I can achive this and using which utility?

sac

Last edited by pawarsac; 08-05-2006 at 02:57 AM.
 
Old 08-05-2006, 02:36 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Doesn't make too much sense to me ... what's the background of this?
You could just read all lines and then delete the whole file? :)


Cheers,
Tink
 
Old 08-05-2006, 03:11 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
here's some concepts
Code:
cat file > tmp
IFS=$'\n'
for a in $(<tmp); do
    # remove the line
    sed -i -n 1\!p file
    # OR sed -i -n /"$a"/\!p file 
done
 
Old 08-05-2006, 05:20 AM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I understand that you want to process each line somehow and have the line removed when you're done with it?
So,
Code:
#! /bin/bash

FILENAME=your_file

while [[ -s "$FILENAME" ]]
do
   read < "$FILENAME"
   sed -i '1d' "$FILENAME"
   echo "$REPLY processed and deleted."
done
To me, this only seems to make sense if other processes may append lines to the file, and in that case you'll need some file locking (in both the reading and writing processes). Have a look at
Code:
#! /bin/bash

FILENAME=your_file

while [[ -s "$FILENAME" ]]
do
  LINE=$( flock "$FILENAME" \
          bash -c "read <'$FILENAME';\
                   sed -i 1d '$FILENAME';\
                   echo \"\$REPLY\"" )
  echo "$LINE processed and deleted."
done
 
Old 08-05-2006, 05:43 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
This can be as simple as:
Code:
while read line <file; do process $line; sed -i '1 d' file; done
Yves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
new to shell programming bluetwist Debian 3 08-01-2005 04:56 PM
Shell Programming eantoranz Linux - Networking 2 06-02-2005 12:21 PM
Shell Programming aman_goel Linux - General 1 11-25-2004 09:50 PM
shell programming athenerx Programming 1 10-28-2001 05:33 AM


All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration