insert string at end of line if it found from list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers insert string at end of line if it found from list
# 1  
Old 09-14-2012
Question insert string at end of line if it found from list

Hi all,

can some one help me out

file 1 i have

Code:
06/01	3:14	d378299
06/01	8:10	d642036
06/01	10:51	d600441
06/01	10:52	d600441
06/01	11:11	d607339
06/01	11:49	d398706

something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706)

Code:
d607339
d600441

so from file1 i have search the contents of list if i find a match i need to print the line follwed by a string match

ie is

Code:
06/01	11:11	d607339 found

pls reply urgetntly

Last edited by Scrutinizer; 09-14-2012 at 08:27 PM.. Reason: code tags
# 2  
Old 09-14-2012
try this..

Code:
grep "d600441" file && echo "found"

Code:
awk '/d600441/{print $0=$0" found"}' file

# 3  
Old 09-14-2012
hi pamu its giving me this error

Code:
 $ awk '/d600441/{print $0=$0" found"}' file.txt
awk: can't set $0
 record number 3

and for the first option it printed only at end of all finds ie aftre 3 times at end in new line it printed found

Last edited by Scrutinizer; 09-14-2012 at 08:27 PM..
# 4  
Old 09-14-2012
Quote:
Originally Posted by zozoo
hi pamu its giving me this error

$ awk '/d600441/{print $0=$0" found"}' file.txt
awk: can't set $0
record number 3

and for the first option it printed only at end of all finds ie aftre 3 times at end in new line it printed found
Try this..

Code:
awk '/d600441/{print $0, "found"}' file

This User Gave Thanks to pamu For This Post:
# 5  
Old 09-14-2012
HI pamu thanks alot it worked Smilie
# 6  
Old 09-14-2012
I must have completely misunderstood what zozoo wanted to do. I thought that given the files list and file1 specified in the first message, you wanted output that looked like:
Code:
06/01 3:14 d378299
06/01 8:10 d642036
06/01 10:51 d600441 found
06/01 10:52 d600441 found
06/01 11:11 d607339 found
06/01 11:49 d398706

If that is what you wanted, I got the above output by running the awk command:
Code:
awk 'FNR==NR {list[$1]
        next }
$3 in list {
        print $0, "found"
        next }
        {print}
' list file1

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

2. Shell Programming and Scripting

Insert specific line when found similar value

Hi All, I have file like this: file1: 3778 10474 24 3778 10475 24 3778 10476 25 3778 10495 26 3794 10001 33 3794 10002 33 3794 10004 33 3794 10007 34 3794 10008 34 3794 10011 34 3794 10012 34 3794 10013 34 3794 10017 34 3810 10282 27 (6 Replies)
Discussion started by: attila
6 Replies

3. Shell Programming and Scripting

gawk (dos) insert backslash at end of line

I have created with the DOS-command dir a list of a directory. Directory of C:\Users\User\Documents 09.06.2011 20:50 48.322 file1.txt 02.11.2010 23:00 9.216 file2.txt 15.12.2010 21:06 26.793 file2.txt Now i would like to add the directory ahead of the filename. Therefore a... (6 Replies)
Discussion started by: sdf
6 Replies

4. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

5. Shell Programming and Scripting

insert text at every end of the line

Hi, I just wanted to know if you have any idea or script to insert a text at everyend of the line, the text will vary. for example sample: this is line1 ok this is line2 ok this is line3 ok output: this is line1 ok /home/line1.txt this is line2 ok /home/line2.txt this is line3 ok... (6 Replies)
Discussion started by: invinzin21
6 Replies

6. UNIX for Dummies Questions & Answers

insert period at the end of each line

file1 contains: this is a test this is a test and only a test this is another test this is another test and only another only i'd like my file to look like this: this is a test. this is a test and only a test. this is another test. this is another test and only another only. (6 Replies)
Discussion started by: tjmannonline
6 Replies

7. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

8. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

9. Shell Programming and Scripting

How to insert a string at the end of a file read

Hi, This thread is an extension of the post I posted in another folder for Unix Dummies. I am actually trying to read all the files in a folder, let say folder A. There are alot of .txt files in this folder where I have sed them and translate to numeric elements. For example:- I have... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question