LinuxQuestions.org
Visit Jeremy's Blog.
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 11-26-2010, 09:42 PM   #1
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Rep: Reputation: 0
extract/delete line from file to other.


hi, guys

I need your help on a piece of code.

i have 2 files.

file1.txt with content:
song1
song2
song3
song4

and an empty file2.txt

and i want to extract and delete randomly a line from file1.txt and put it on file2.txt

the result would be:

file1.txt with content:
song1
song2
song4

file2.txt with content:
song3
 
Old 11-26-2010, 10:48 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
Hi and Welcome to LQ.

Ok, where exactly are you stuck?
 
Old 11-26-2010, 11:11 PM   #3
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 0
I'm stuck with the command sed can't make it working
 
Old 11-26-2010, 11:29 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
So what sed have you tried? Are you able to do the 2 individual tasks? ie print line to other file and delete line from first file.
 
Old 11-26-2010, 11:29 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
Quote:
Originally Posted by killerwake View Post
I'm stuck with the command sed can't make it working
So what's your code so far?
 
Old 11-27-2010, 12:06 AM   #6
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 0
i don't have it yet, i know that sed can't remove line with an expression sed '/Hello/d' file.txt but i can't implement on the way to get a random line and put it on the other file!
 
Old 11-27-2010, 12:23 AM   #7
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 0
i'm stuck in this, with this sed line i can get a random line and print on file2.txt but i can't insert this as variable on sed to delete the line.

GETLINE=$(sed -n $((1+$RANDOM%`wc -l file1.txt | cut -f 1 -d ' '`))p file1.txt > file2.txt)

sed -e "/$GETLINE/d" file1.txt > file1_.txt

mv file1_.txt file1.txt

this is to run every 30 minutes and when file1.txt gets empty do:

find /home/mp3/ -type f -name "*.mp3" > file1.txt

Last edited by killerwake; 11-27-2010 at 12:25 AM.
 
Old 11-27-2010, 12:32 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
It might be better to calculate a random line number (within range), and then use:

sed -n "${num}p" text1 >> text2
sed -i "${num}d" text1

You don't use slashes around the line number for one thing.


Break up the problem so that your calculations is outside the sed command.

declare -i lines random_line
lines=$(($(wc -l text1)))
random_line=$((${RANDOM}%${lines}+1))

There will be an error here. Declaring lines, and random_lines helped pick up the mistake.
..
declare -i lines random_line
lines=$(($(wc -l text1.txt | cut -d' ' -f1)))
random_line=$(( ${RANDOM%${lines}+1 ))

That's better.

sed -n "${random_line}p" file1.txt >> file2.txt
sed -i "${random_line}d" file1.txt

A common catch is to not use $(( ... )) when dealing with integers.

Last edited by jschiwal; 11-27-2010 at 01:03 AM.
 
Old 11-27-2010, 12:41 AM   #9
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 0
jschiwal impossible to do that because the total of number of lines it's not always equal. but thanks anyway, your code gave me a better ideia.

i found the solution:

N=$((1+$RANDOM%`wc -l file1.txt | cut -f 1 -d ' '`))

sed -n "${N}p" file1.txt >> file2.txt

sed -i "${N}d" file1.txt


now i only got to implement an if statement when file1.txt gets empty.
 
Old 11-27-2010, 12:52 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
I thought you might like this tidy up:
Code:
(( N = 1 + RANDOM % $(cat file1.txt | wc -l) ))
 
Old 11-27-2010, 12:58 AM   #11
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 0
grail: don't you have to put the cut -f 1 -d ' ' part to remove the empty line on the file????
 
Old 11-27-2010, 01:16 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
No you are using the cut because if you do:
Code:
wc -l file1.txt
The output is:
Code:
10 file1.txt
However when using the cat command, wc see the input as coming from stdin and output is:
Code:
cat file1.txt | wc -l
10
 
Old 11-27-2010, 01:37 AM   #13
killerwake
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 0
ok got it!

SOLVED!!!!!!!!!!!!!!!!
 
Old 11-27-2010, 02:37 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Cool ... look under thread tools to mark as SOLVED.
 
Old 11-27-2010, 02:49 AM   #15
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by killerwake View Post
jschiwal impossible to do that because the total of number of lines it's not always equal. but thanks anyway, your code gave me a better ideia.

i found the solution:

N=$((1+$RANDOM%`wc -l file1.txt | cut -f 1 -d ' '`))

sed -n "${N}p" file1.txt >> file2.txt

sed -i "${N}d" file1.txt


now i only got to implement an if statement when file1.txt gets empty.
Yes you need to count the number of lines each time.
My main point is that separating the tasks into separate lines can make debugging easier. Also use "declare -i" for integer variables to expose hidden errors where a string would be assigned to the variable.

Last edited by jschiwal; 11-27-2010 at 02:51 AM.
 
  


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
Delete Line from File mkohler Linux - Newbie 5 11-11-2010 08:33 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07:46 PM
How do I extract a file on the command line Felixone Linux - Newbie 5 10-01-2008 11:14 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 06:34 PM
Delete line from file nebbus Linux - Newbie 3 06-03-2006 01:46 PM


All times are GMT -5. The time now is 10: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