Append using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append using awk
# 1  
Old 11-01-2007
Append using awk

Hi All,

I have a problem with my awk script. I am trying to append the whole line to another file called " file.txt " but i can't. I suppose there's something wrong in the way that i pipe. I canot use " nawk -f awk_script input > file.txt " because i have a 2nd input.
Can any expert give some advise?

Input:
wwwwww
0999 k= 1
0999 bb= 111
wwwwww
** QQQAAAR1 ccc ccc cc cc ccc 2007
wwwwww
wwwwww
0001 k= 1
0001 bb= 222
wwwwww
0002 k= 1
0002 *bb= 333
** abc ccc cc cc cc cc 2007
wwwwww
0001 k= 1
0001 bb= 444
wwwwww
0002 k= 1
0002 bb= 555
wwwwww
wwwwww
0003 k= 1
0003 bb= 666
wwwwww
** QQQAAAR1 ccc ccc cc cc ccc 2007
wwwwww
0003 k= 1
0003 bb= 777
wwwwww
0004 k= 1
0004 *bb= 888
0005 k= 1
0005 bb= 999


Code:
FNR==NR && $1 == "**" && $2 ~ /^QQQ/ && $2 ~ /R1$/ && $8 == "2007" {
CODE = $2 ;print $0|getline > file.txt ; next};

# 2  
Old 11-01-2007
Code:
nawk 'NR == FNR && /^** QQQ[^ ]*R1 .* 2007$/ {
	CODE = $2
	print > "file.txt"
	next
}' filename

# 3  
Old 11-03-2007
Quote:
Originally Posted by radoulov
Code:
nawk 'NR == FNR && /^** QQQ[^ ]*R1 .* 2007$/ {
	CODE = $2
	print > "file.txt"
	next
}' filename

Thanks radoulov!!! It worked!!
But i don;t really understand this portion of the code " /^** QQQ[^ ]*R1 .* 2007$/ " .
Can you help me understand it better ?
# 4  
Old 11-04-2007
Quote:
Originally Posted by Raynon
[...]
But i don;t really understand this portion of the code " /^** QQQ[^ ]*R1 .* 2007$/ " .
It's a regular expression that matches the records that
begin with ** (^ anchors the beginning of the line)
followed by QQQ, then [^ ]* means zero or more occurrences
of characters other than a space: "^" as the first character
after the "[" indicates character(s) except those
in square brackets, followed by a literal R1, space and
zero or more occurrences of any character followed by
a space and 2007 at the end of the line.
# 5  
Old 11-04-2007
Thanks radoulov!!!
You are such a pro at this!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append header with awk

I have to append the header (ie "START OF LINE") to a file only if there is data in it. Below command is showing header though there is no data in a file, can you suggest how to correct.. awk '{NR!=0} BEGIN{print "START OF LINE"}; {print}' file (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

Match and append - awk

ALL, Please help with this ... File1 000433,ds00d11,tdev,ds00d11_view,0CD3 000433,ds00d12_34,tdev,ds00d12_view,132D File2 CG01_ds00d11_drs,rs1_ds00d11_0CD3_114D,000433,0CD3 CG01_ds00d11_drs,rs1_ds00d11_0CD3_114D,000566,114D ... (6 Replies)
Discussion started by: greycells
6 Replies

3. Shell Programming and Scripting

Awk: Append new elements to an array

Hi all, I'm dealing with a bash script to merge the elements of a set of files and counting how many times each element is present. The last field is the file name. Sample files: head -5 *.tab==> 3J373_P15Ac1y2_01_LS.tab <== chr1 1956362 1956362 G A hom ... (7 Replies)
Discussion started by: lsantome
7 Replies

4. Shell Programming and Scripting

awk to append data to a file

Hi I search for certain values in a file across many directories using the following awk code awk '/Sl.*thickness/ {Sl=$3;Tt=$NF}END{print Sl, Tt}' DILAT.DAT What I would like to do is write out Sl and Tt obtained from these files from many directories to a single file. So for example if... (2 Replies)
Discussion started by: lost.identity
2 Replies

5. UNIX for Advanced & Expert Users

awk if/substring/append help

Hi All, I need some help with an awk command: What I'm trying to do is append "MYGROUP: " to text with the substring "AT_" the input file follows this format: AT_xxxxxx Name1 Name2 AT_xxxxxx NameA NameB I want the output to be: MYGROUP: AT_xxxxx Name1 Name2 MYGROUP:... (2 Replies)
Discussion started by: bikecraft
2 Replies

6. UNIX for Dummies Questions & Answers

how do I append new lines to awk variable?

I want to build an array using awk, consisting only of a subset of lines of a file. I know how to have awk assess whether a key phrase is in a particular line, but I can't find anywhere how to then append the line containing that phrase to an array that has previously-found lines also containing... (9 Replies)
Discussion started by: pts2
9 Replies

7. Shell Programming and Scripting

Append using SED/AWK

Hi friends, I have a file with content SOME TEXT HERE I want to append the string GREAT to Line 1 of my file such that the file content should be GREAT SOME TEXT HERE I can do a cat for the string great and file >> newfile and then rename newfile name to file But this does not put... (5 Replies)
Discussion started by: dahlia84
5 Replies

8. Shell Programming and Scripting

awk append to one line if first field is the same

Hi all, How would I append the second field each time to one line if the first field is the same for example I have this data: 10430,187976 10430,251588 10430,262904 10430,275008 10430,279892 10430,275008 10430,303740 10430,318136 10430,336988 10430,350324 10430,373648 And I... (4 Replies)
Discussion started by: borderblaster
4 Replies

9. UNIX for Dummies Questions & Answers

use awk to append values

Hi, I have a file like this: tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 and what i want is: value1 value2 value3 value1 value2 value3 (15 Replies)
Discussion started by: nickrick
15 Replies

10. Shell Programming and Scripting

how to append into array thru awk

hey champs, i have variable as field_dtls, which has values like CLIENT ID|FAMILY NAME|MIDDLE NAME|FIRST NAME|COUNTRY NAME|ADDRESS|NATIONAL ID|PASSPORT NUMBER so, echo "$field_dtls" CLIENT ID|FAMILY NAME|MIDDLE NAME|FIRST NAME|COUNTRY NAME|ADDRESS|NATIONAL ID|PASSPORT NUMBER but i... (2 Replies)
Discussion started by: manas_ranjan
2 Replies
Login or Register to Ask a Question