Using awk to get a line number to delete, piping through sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using awk to get a line number to delete, piping through sed
# 1  
Old 03-30-2009
Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code:

Code:
echo 'Please input first name: \c'
read searchFirst
echo 'Please input last name: \c'
read searchLast

egrep -in "$searchLast" contacts.txt | egrep -i "$searchFirst" | awk -f: 'NR > 0 && NR < $NR { print $1 }'

I successfully get the line number that awk prints, but I can't figure out how I can pipe that to a sed in order to delete it. I've tried to delete it using the $1 variable from the awk, but I don't think it's possible to access that variable outside of the awk. Is there a way to incorporate the sed directly into the awk? Thanks.
# 2  
Old 03-30-2009
Yeah, I overcomplicated it.

Code:
egrep -iv "$searchFirst":"$searchLast" contacts.txt > contacts.txt

Did the trick.
# 3  
Old 03-30-2009
Alright, nevermind. When I do contacts.txt > contacts.txt it erases my contacts file. If I do

Code:
egrep -iv "$searchFirst":"$searchLast" contacts.txt | tee contacts.txt

It works, but I don't want the entries that don't match to be printed on screen. Any suggestions?
# 4  
Old 03-30-2009
Nevermind.

Code:
egrep -iv "$searchFirst":"$searchLast" contacts.txt > tempFile
mv tempFile contacts.txt

There's not a cleaner way of doing that, is there?
# 5  
Old 03-30-2009
It is always good to be smart enough to answer your own questions Smilie

I do not know of a way to get egrep to overwrite the original file. If you use Perl it has a command line function that will create a backup of your original and then update the original. Although, I do not see much of an advantage over just using ';' semi-colon so you can execute both of your commands on the same line.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If the value > 100 delete the line with awk or sed

if the value > 100 delete the line delay time: 42.978 ms delay time: 43.684 ms delay time: 41.082 ms delay time: 44.845 ms delay time: 40000.057 ms delay time: 41.158 ms delay time: 42.239 ms delay time: 50.579 ms delay time: 46.334 ms to (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. UNIX for Advanced & Expert Users

Problem piping find output to awk, 1st line filename is truncated, other lines are fine.

Today I needed to take a look through a load of large backup files, so I wrote the following line to find them, order them by size, and print the file sizes in GB along with the filename. What happened was odd, the output was all as expected except for the first output line which had the filename... (4 Replies)
Discussion started by: gencon
4 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

5. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

6. Shell Programming and Scripting

Delete line with sed or awk (with specified condition)

Hello. I'm trying to delete the lines of a file does not contain the letter "T " (for example) at position 26. So far, I could only print the result: awk '{if (substr ($ 1,1,26)! ~ / T /) print}' file.txt How I can do to eliminate the lines that meet this condition? Help please. ... (4 Replies)
Discussion started by: </kida>
4 Replies

7. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

8. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies
Login or Register to Ask a Question