Use loop in sed or awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use loop in sed or awk?
# 1  
Old 10-23-2008
Use loop in sed or awk?

I know a little bit of sed and awk, but not too much. can loop be done in sed.
Baiscally what I want is:
file1:
aaa 111
bbb 222
ccc 333
ddd 444
eee 555
fff 666
Note: the space in between is "tab"
file2:
bbb
ddd
eee
I want delete any entries in file1 which starts with entries in file2. The output I want is:
aaa 111
ccc 333
fff 666
I wrote a script by using "read" command, it works but all the "tab"s replaced by space.
I'm looking for the solution of sed or awk. but don't know how to use loop inside sed. Or any better way to achieve this. Please help.
Thanks...
# 2  
Old 10-23-2008
Code:
grep -v -f file2  file1

# 3  
Old 10-23-2008
Oh. Silly me! I have been using grep for such a long time but never known -f option.
Just tried it on solairs box. Figured -f is only valid for /usr/xpg4/bin/grep, but not /usr/bin/grep. Do you know the different of these two "grep"?

Many THX!
# 4  
Old 10-23-2008
You are on Solaris. XPG4 was an early standards attempt for all of UNIX -- at what POSIX is supposed to do now.

Don't use old awk or any other oldies.
# 5  
Old 10-23-2008
you're the man!
That's why I like UNIX. There are always something new for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

4. UNIX for Dummies Questions & Answers

Using sed in a for loop

Hi, I have a file called 1.txt, I want to create 2.txt through 100.txt using a for loop. In each instance of the loop, I want to change all "1"'s in the text file to "2"s and so on... I tried the code below to do this: for i in {2..100}; do sed 's/1/$i/g' 1.txt > $i.txt; done but it... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Using sed in a for loop

Hello I have a group of files a1.profile a2.profile a3.profile a4.profile b1.profile b2.profile b3.profile b4.profile These files all have the same first line with a value s1 atop the columns s1_context s1_ref s1_sample s1_% etc I am trying to use sed in a for loop to replace the s1 in the... (2 Replies)
Discussion started by: plumb_r
2 Replies

6. Shell Programming and Scripting

SED/AWK maybe PERL in a WHILE loop

Hi, I have a file containing numerous string values, for example: AMFU8849636 CCLU7120334 GESU5784065 TEMU3070096I have a second file with multiple records, such as the one below that I would like to manipulate based on the strings from the first file: CATOS2EDI DSTOW ABC ... (7 Replies)
Discussion started by: doza22
7 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

Using sed in for loop

I have a file MAT.txt which contains the following data: mat1.txt mat2.txt . . . . mat100.txt I want to remove the '.txt' from every line and have an output file with the following data: mat1 mat2 . . . . mat100 I know this can be done with sed easily for each line, but I do... (8 Replies)
Discussion started by: ursaan
8 Replies

9. Shell Programming and Scripting

For loop with sed

Hi there, I have 1 file with different 144 lines and 144 files that I want to change with a sed. What I want to do is to go trough the file with the 144 different lines take the line a replace a certain pattern with said in the first file of the folder where the 144 files are. Take the second... (3 Replies)
Discussion started by: sickboy
3 Replies

10. Shell Programming and Scripting

sed with awk in loop

Hi Guys, I am using following code to do some task. Can somebody please help me to use a for loop for this ? ___________________________________________________ sed s/DATA2/`awk '/ / {print $1}' formated1`/ EOD_Batch > EOD_Batch1 sed s/DATA3/`awk '/ / {print $2}' formated1`/ EOD_Batch1 >... (4 Replies)
Discussion started by: manchau
4 Replies
Login or Register to Ask a Question