use awk to append values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers use awk to append values
# 8  
Old 08-15-2008
or with just awk:

Code:
# awk '{FS=":";a=$2;getline;b=$2;getline;c=$2;printf "%s %s %s\n",a,b,c;getline}' file
value1 value2 value3
value1 value2 value3
value1 value2 value3

# 9  
Old 08-15-2008
This awk example would be a little ungainly with 31 parameters, what i've been trying to do is something like this, but build an array and then print out the array to a single line when I reach an empty line in the input. A bit like era's example earlier, but matching the output from your cut and paste example.

Any ideas?

The cut and paste has worked, but i'm interested in how this could be achieved in awk.

regards,
nick
# 10  
Old 08-15-2008
I'll switch to nawk:

Code:
#  nawk -F: '$0!=""{printf "%s ", $2;next}1' file

value1 value2 value3
value1 value2 value3
value1 value2 value3

# 11  
Old 08-15-2008
That looks like a neat solution.

Unfortunately we don't have nawk on our environment for some reason. (just scanned the server to make sure)

How would you do this with awk?
# 12  
Old 08-15-2008
i think in this case instead of nawk you can use awk also..
i mean
awk -F: '$0!=""{printf "%s ", $2;next}1' file
# 13  
Old 08-15-2008
Code:
awk 'BEGIN{RS=""; FS=":"}
{
   for (i=2; i<=NF; i+=2)
      printf("%s ", $i)
   print ""
}' file

# 14  
Old 08-15-2008
Quote:
Originally Posted by nickrick
That looks like a neat solution.

Unfortunately we don't have nawk on our environment for some reason. (just scanned the server to make sure)

How would you do this with awk?
how about

Code:
[#  /usr/xpg4/bin/awk -F: '$0!=""{printf "%s ", $2;next}1' file

 
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