use awk to append values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers use awk to append values
# 1  
Old 08-15-2008
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
value1 value2 value3

that is extract a set of values from paragraphs and append thoses values into a delimited list, one row per paragraph. I then intend to use awk to format the data in the rows in multiple ways.

any tips? I've struggled with various sed and awk ideas for hours and not getting anywhere fast.

Any help greatly appreciated.

Nick
# 2  
Old 08-15-2008
sed -e '/./N;s/\n/ /g;;/./N;s/\n/ /g;s/tag.://g;/^$/d' file name
try this.. i din't get your full problem..
# 3  
Old 08-15-2008
Your example is not clear. Do you mean x1:a1 x2:b1 x3:c1 x1:a2 x2:b2 x3:c2 should map to a1 a2; b1 b2; c1 c2, or what?

Code:
awk -F : '{ tag[$1] = tag[$1] " " $2 } END { for (t in tag) print tag[t] }' file

There will be a leading space on the output lines; it can be avoided, of course, but maybe it's simpler to just post-process the output if necessary.
# 4  
Old 08-15-2008
Sorry for not explaining it clearly. Having looked at your examples I don't think they quite meet what I wanted, but they're not far off. so..

I have a file that contains a set of repeating paragraphs , seperated by an empty line, like this:

layer:1
property1:x1
property2:y1
property3:z1

layer:2
property1:x2
property2:y2
property3:z2

layer:3
...

what I want is for a new file to be created with one row per paragraph, contining just the values of each paragraph . i.e.

1 x1 y1 z1
2 x2 y2 z2
3 x3 y3 z3

does this clarify the problem?
# 5  
Old 08-15-2008
sed -e '/./N;s/\n/ /g;/./N;s/\n/ /g;/./N;s/\n/ /g;s/*.://g;/^$/d' filename
will it do??
# 6  
Old 08-15-2008
Code:
#  cut -d":" -f2 file | paste - - - -
value1  value2  value3
value1  value2  value3
value1  value2  value3

where file =

Code:
value1
value2
value3

value1
value2
value3

value1
value2
value3

# 7  
Old 08-15-2008
The simple ones are the best Tytalus!

I need 31 '-' in my paste but, it'll make the code far easier to maintain in the future.

I geuss this only works if i have a consistent set of value's in a paragraph though - which I do - so it's great.

Doesn't help me get my head around sed and awk though!

many thanks for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Bash append values if keywords are present in the file

Hi Team, i have a web ui where user will be passing values and the output will be saved to a file say test with the following contents . These below mentioned values will change according to the user_input Just gave here one example Contents of file test is given below Gateway... (7 Replies)
Discussion started by: venkitesh
7 Replies

3. Shell Programming and Scripting

<< generate alphabets and append in the input values >>

Hi Team, Pls help to get the desired output. I have a input like below nodecount=10 host=na7-db1-1-chi nodecount can be 10 or 8 based on this we need a output (in single line) like below na7-db1-1-chi:A na7-db1-2-chi:B na7-db1-3-chi:C na7-db1-4-chi:D na7-db1-5-chi:E... (4 Replies)
Discussion started by: kamauv234
4 Replies

4. Shell Programming and Scripting

Match value in column and append file with new values

Hi, I need help to match two files based on two columns. file_1 ID AA An Ca Ele Pro Su Ot Tra g13950 No No Yes No Yes Yes Yes Yes g05760 Yes No No No No Yes Yes Yes g12640 No No No No No No No No k17720 No Yes No No No No No Yes g05640 Yes Yes Yes No No Yes Yes Yes file_2 ... (8 Replies)
Discussion started by: redse171
8 Replies

5. Shell Programming and Scripting

Append values of duplicate entries

My input file is: LOC_Os01g01870 GO:0006139 LOC_Os01g01870 GO:0009058 LOC_Os01g02570 GO:0006464 LOC_Os01g02570 GO:0009987 LOC_Os01g02570 GO:0008152 LOC_Os01g04380 GO:0006950 LOC_Os01g04380 GO:0009628 I want to append the duplicate values in a tab/space... (2 Replies)
Discussion started by: Sanchari
2 Replies

6. UNIX for Dummies Questions & Answers

How to append values to a string?

Hi, Requesting some help with a problem I am facing with string function in UNIX. I wish to create 2 string variables: 1st header string containing output_1, output_2, .. , output_<n> and 2nd data string containing the filename separated by colon (":") and corresponding filesize separated by... (6 Replies)
Discussion started by: vkumbhakarna
6 Replies

7. AIX

How to append spaces to string values?

i/o file: abc,efg,xyz Required o/p file: "abc (Value + blank spaces=16) " ,"efg (Value +blank spaces=15) " ,"xyz (Value+ blank spaces =20) " In short input file value stores in result file with " i/p Value " added with spaces and are of fixed size like 16,15,20 How to do using... (2 Replies)
Discussion started by: AhmedLakadkutta
2 Replies

8. Shell Programming and Scripting

Need help with shell, trying to append or separate values in a string

Ok. I for the life of me cant figure out how to do this. I need Help. So here is what I'm trying to do. I have a block of text. They are FIPS codes for counties. Below is the block. There are probably a few ways to do this. The first line starting with ARC021....... this line is a list of... (2 Replies)
Discussion started by: chagan02
2 Replies

9. UNIX for Dummies Questions & Answers

Match values from 2 files and append certain fields

Hi, I need help on appending certain field in my file1.txt based on matched patterns in file2.txt using awk or sed. The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly. ... (1 Reply)
Discussion started by: redse171
1 Replies

10. Shell Programming and Scripting

Append values before a string

hi all, i have variables a and b with values, like a="/var/tmp/new.sh /var/tmp/new2.sh" b="/TEST" how i need to append the value "/TEST" before the values for the variable "a" so that i get the output as /TEST/var/tmp/new.sh /TEST/var/tmp/new2.sh plz help me Regards, NG (2 Replies)
Discussion started by: Nandagopal
2 Replies
Login or Register to Ask a Question