Find two words and join together in one file

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Find two words and join together in one file
# 8  
Old 06-03-2010
Hi umapearl,

Your second file has a different format than the one originally specified. Try this:
Code:
awk '/^Fatal/{sub(/\.$/,"");p=$0}p&&/^search does not exist/{print p" is id = "$8;p=""}' infile

output:
Code:
Fatal Error for input record 27889 is id = 2396539
Fatal Error for input record 27892 is id = 2396540
Fatal Error for input record 27893 is id = 2396541
Fatal Error for input record 27894 is id = 2396542
Fatal Error for input record 27895 is id = 2396543
Fatal Error for input record 27896 is id = 2396544
Fatal Error for input record 27901 is id = 2396545

# 9  
Old 06-04-2010
Hi Thanks a lot,

My file consist of both type , so is it possible to merge both the awk statements. Sorry for disturbing. Please help.

Thanks
# 10  
Old 06-04-2010
Hi, this would combine the two criteria:
Code:
awk '/for input record/          {p=""}
     /^Fatal/                    {sub(/\.$/," is id = ");p=$0}
     p&&/^Record/                {print p$17}
     p&&/^search does not exist/ {print p$8}' infile

Output:
Code:
Fatal Error for input record 25 is id = 543523
Fatal Error for input record 35 is id = 643523
Fatal Error for input record 27889 is id = 2396539
Fatal Error for input record 27892 is id = 2396540
Fatal Error for input record 27893 is id = 2396541
Fatal Error for input record 27894 is id = 2396542
Fatal Error for input record 27895 is id = 2396543
Fatal Error for input record 27896 is id = 2396544
Fatal Error for input record 27901 is id = 2396545

# 11  
Old 07-01-2010
friend, your posts are bit too long.

just tell me what is your input and output.
# 12  
Old 07-16-2010
Does this work for you?

Code:
awk '
/^Fatal / {
	if (FatalMsg != "")
	    printf "%s, %s\n", FatalMsg, "No ID found"
	FatalMsg = $0
	sub(/\.$/, "", FatalMsg)
}
/ id = / {
	if (FatalMsg == "")
	    next
	sub(/^.* id =  */, "")
	sub(/ .*/, "")
	IdNum = $0
	printf "%s, id = %s\n", FatalMsg, IdNum
	FatalMsg = IdNum = ""
}
END {
	if (FatalMsg != "")
	    printf "%s, %s\n", FatalMsg, "No ID found"
}
' data

Moderator's Comments:
Mod Comment Please use code tags, thank you!

Last edited by Franklin52; 07-16-2010 at 04:05 PM..
# 13  
Old 07-30-2010
Sorry for the late reply,

Thanks a lot for all your help,My problem is sovled. could anyone help me to show how to use regex.

Thanks once again
# 14  
Old 07-31-2010
Regular expressions are a pretty big subject. You may find this recent thread useful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

3. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

4. UNIX for Dummies Questions & Answers

Find a file with common initials and last words

Hi, I have a requirement like i have to find out files and remove them on a daily basis. The files are generated as abc_jnfn_201404230004.csv abc_jnfo_201404230004.csv abc_jnfp_201404230004.csv abc_jnfq_201404230004.csv abd_jnfn_201404220004.csv abe_jnfn_201404220004.csv i want to... (1 Reply)
Discussion started by: Mohammed_Tabish
1 Replies

5. Shell Programming and Scripting

Find Multiple words in a file

Hi all, I have trouble in finding the multiple word in .txt file. Please help me with any solution. I have 10,000 .txt files and in each file i have to search specific word but more than one, like (data, machine learning, clustering) and all these keywords should be case insensitive because... (3 Replies)
Discussion started by: XXLMMN
3 Replies

6. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

7. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. Shell Programming and Scripting

Script to find all the files that contain any of the words present in another file

Hi All, I am new to UNIX and shell scripts and also new to this forum. I need a script to find all the files in a directory that contain any of the strings present in another file. Please provide me the script or if you could provide pointers to any link in this forum it would be helpful.... (4 Replies)
Discussion started by: tsanthosh
4 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question