copy, then delete lines in file with sed using a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy, then delete lines in file with sed using a pattern
# 1  
Old 02-22-2011
copy, then delete lines in file with sed using a pattern

I need to copy lines to a new file from files with sed using a pattern in char postions 1-3.

Then after the copy, I need to delete those same lines from the input files.

For example, string "ABC" in pos 1-3
# 2  
Old 02-22-2011
Please, specify a sample of your input data and expected output.
# 3  
Old 02-22-2011
Could you give an example?
# 4  
Old 02-22-2011
sample input file 1:
Code:
ABC9875193458739457 LKSDJSG 983475394578 LSKJFG
QZS0987983758934584 OIOISDJ 023984234244 IOEUYR
QZS0987591485794788 OIOISDJ 023984234244 IOEUYR

sample input file 2:
Code:
QZS0988347653564788 OIOISDJ 023984234244 IOEUYR
ABC9898751345345457 LKSDJSG 983475394578 LSKJFG
QZS0987236523652346 OIOISDJ 023984234244 IOEUYR

sample output file:
Code:
ABC9875193458739457 LKSDJSG 983475394578 LSKJFG
ABC9898751345345457 LKSDJSG 983475394578 LSKJFG

^and these lines removed from input 1&2
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-22-2011 at 05:24 PM.. Reason: code tags, please!
# 5  
Old 02-22-2011
To copy lines starting with "ABC" into a new file:
Code:
egrep "^ABC" input_file > output_file

To delete lines starting with "ABC" into another file:
Code:
egrep -v "^ABC" input_file > output_file

This User Gave Thanks to Shell_Life For This Post:
# 6  
Old 02-22-2011
Code:
sed -i '/^ABC/W output
/^ABC/d'  file1 file2

This User Gave Thanks to yinyuemi For This Post:
# 7  
Old 02-22-2011
I only want ABC from char pos 1-3 because it could be elsewhere in the file and elsewhere it should be ignored.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to delete a pattern in a file

Hi Everyone, I have an unusual requirement. Here is where i am stuck for sometime now... I have this text file.. lets say .. output.sql... it has lot of entries... here below is part of the entry... .. . . . . . . . . . . . . . . . .... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

sed search pattern and delete lines

Hello, i have a question. My problem is that i have a file like: TEST JOHN ADAM MICHAEL SEBASTIAN ANDY i want find for MICHAEL and want delete lines like this: TEST (4 Replies)
Discussion started by: eightball
4 Replies

3. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

4. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

5. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

6. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

7. HP-UX

How to delete specific pattern in a file with SED?

I have one file which is having content as following... 0513468211,,,,20091208,084005,5,,2,3699310, 0206554475,,,,20090327,123634,85,,2,15615533 0206554475,,,,20090327,134431,554,,2,7246177 0103000300,,,,20090523,115501,89,,2,3869929 0736454328,,,,20091208,084005,75,,2,3699546... (7 Replies)
Discussion started by: ganesh.mandlik
7 Replies

8. Shell Programming and Scripting

delete lines in file matching a pattern

I have a text file, a sample of which is as follows: r/- * 0: WINDOWS/Microsoft.NET/Framework/v2.0.50727/ASP.NETWebAdminFiles/Images/headerGRADIENT_Tall.gif r/- * 0: WINDOWS/SoftwareDistribution/Download/cf8ec753e88561d2ddb53e183dc05c3e/backoff.jpg r/- * 0: ... (2 Replies)
Discussion started by: stumpyuk
2 Replies

9. Shell Programming and Scripting

sed delete pattern skipping first n lines of file.

I have files of more than 10K lines that I need to delete lines that contain a pattern, but I want to keep the first few lines intact. Can this be done with sed? (7 Replies)
Discussion started by: tkg
7 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question