Search multiple strings on a file and copy the string next to it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search multiple strings on a file and copy the string next to it
# 1  
Old 06-26-2009
Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this?

---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ----------

my working file looks like this:

Code:
<empty>
<empty>
<empty>
NAME :ABC        AGE :15
GENDER :MALE
<other strings> 
<other strings>   
NAME :DEF       AGE :18
GENDER :FEMALE
<other strings> 
<other strings>   
NAME :GHI       AGE :5
GENDER :FEMALE 
<other strings> 
<other strings>

output file should be:
Code:
ABC|15|MALE
DEF|18|FEMALE
GHI|5|FEMALE

i used this code:

awk -n 'data.awk > output

data.awk looks like this
Code:
{
	if (substr($1,1,4) == "NAME")
         {  
 	   name = substr($2,2) ; age = substr($4,2)
	   }
	if (substr($1,1,6) == "GENDER")
         {  
 	   gender = substr($2,2)
	   }
print name"|"age"|"gender
}

but it outputs like this:
Code:
||
||
ABC|15|MALE
ABC|15|MALE
ABC|15|MALE
ABC|15|MALE
DEF|18|FEMALE
DEF|18|FEMALE
DEF|18|FEMALE
DEF|18|FEMALE
GHI|5|FEMALE
GHI|5|FEMALE
GHI|5|FEMALE
GHI|5|FEMALE
||
||

what could be the problem?

Last edited by vgersh99; 06-26-2009 at 09:53 AM.. Reason: code tags, PLEASE!
# 2  
Old 06-26-2009
Code:
BEGIN {
  FS="[ :]"
  OFS="|"
}
$1 == "NAME" { name =$3; age=$NF}
$1 == "GENDER" { print name, age, $NF}

Please start using code tags - it'll improve the chances of someone actually looking at your posts.
# 3  
Old 06-27-2009
Thanks a lot! Il keep ur advice in mind

---------- Post updated 06-27-09 at 12:03 PM ---------- Previous update was 06-26-09 at 08:49 PM ----------

Hi! What do u mean by $NF? Tnx
# 4  
Old 06-27-2009
Code:
awk '
/NAME/{
 for(i=2;i<=NF;i+=2){
   gsub(":","",$i)
   printf "%s|",$i
 }
}
/GENDER/{gsub(/GENDER.*:/,"");print}
' file

i suggest you start reading up on gawk to understand its basics.
# 5  
Old 06-27-2009
@ ghostdog: how about 'age'? Im sorry, im new in unix. There are so much for me to learn. Can you do me a favor of explaining the code above? Thank u so much
# 6  
Old 06-27-2009
why don't you run it and see ?
# 7  
Old 06-27-2009
Code:
nawk 'BEGIN{
FS="[ :]"
}
/NAME/{
tmp=sprintf("%s|%s",$3,$NF)
getline
tmp=sprintf("%s|%s",tmp,$3)
print tmp
}' yourfile


perl:

Code:
$/="NAME";
while(<DATA>){
	print "$1|$2|$3\n" if /:([^ ]+)\s+AGE\s*:([^ \n]+).*GENDER\s*:([^ \n]+)/s;
}
__DATA__
<empty>
<empty>
<empty>
NAME :ABC        AGE :15
GENDER :MALE
<other strings>
<other strings>
NAME :DEF       AGE :18
GENDER :FEMALE
<other strings>
<other strings>
NAME :GHI       AGE :5
GENDER :FEMALE
<other strings>
<other strings>


Last edited by summer_cherry; 06-27-2009 at 06:10 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

To search multiple string in file

hi , i am having a file where i need to take ignore the data from file1.txt and redirect to another file for eg: file1.txt AUS USA file2.txt AUS,123 NZ,11 USA,12 i am using the below code (4 Replies)
Discussion started by: rohit_shinez
4 Replies

4. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

5. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

6. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

7. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

8. Shell Programming and Scripting

Search multiple Strings in a File

Hi I want to search multiple strings in a file . But the search should start with "From" Keyword and end with before "Where" keyword. Please suggest me. Thanks (2 Replies)
Discussion started by: sboss
2 Replies

9. Shell Programming and Scripting

How to search multiple strings in a file

Hi All, I want to search all the ksh scripts that has following details. 1. Search for "exit 0" 2. Search for "sqlldr" or sqlplus" 3. In the above files i want to search for all the script that has no "case" in it. Please advice. Thanks, Deep (2 Replies)
Discussion started by: deepakpv
2 Replies

10. Shell Programming and Scripting

Search for strings & copy to new file

Hi All, I am just learning shell programming, I need to do the following in my shell script. Search a given log file for two\more strings. If the the two\more strings are found then write it to a outputfile else if only one of the string is found, write the found string in one output... (2 Replies)
Discussion started by: amitrajvarma
2 Replies
Login or Register to Ask a Question