Print output as columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print output as columns
# 1  
Old 05-15-2013
Print output as columns

Normal grep is not working to get the output.

Code:
Sample Input:

newjob: abc
command name: a=b+c
newjob: bbc
command name: c=r+v
newjob:ddc
newjob:kkc
command name: c=l+g
newjob:mdc
newjob:ldc
newjob:kjc
command name: u=dl+g
newjob:lkdc
newjob:lksdc
command name: o=udl+g

Sample output:

NEWJOB~COMMAND
abc~a=b+c
bbc~c=r+v
ddc~
kkc~c=l+g
mdc~
ldc~
kjc~u=dl+g
lkdc~
lksdc~o=udl+g

# 2  
Old 05-15-2013
Code:
while read line ; do [ `echo $line | grep "newjob" -c` -gt 0 ] && echo $line | awk -F":" '{print $2"~"}'; [ `echo $line | grep "command" -c` -gt 0 ] && echo $line | awk -F":" '{print $2}' ;done < inputFile

Quote:
Output
abc~
a=b+c
bbc~
c=r+v
ddc~
kkc~
c=l+g
mdc~
ldc~
kjc~
u=dl+g
lkdc~
lksdc~
o=udl+g
Is this OK for you ?
[I know the script is little long !]

Last edited by linuxadmin; 05-15-2013 at 04:36 AM..
# 3  
Old 05-15-2013
Quote:
Originally Posted by linuxadmin
Code:
while read line ; do [ `echo $line | grep "newjob" -c` -gt 0 ] && echo $line | awk -F":" '{print $2"~"}'; [ `echo $line | grep "command" -c` -gt 0 ] && echo $line | awk -F":" '{print $2}' ;done < inputFile


Is this OK for you ?
[I know the script is little long !]
above can be done just by awk

Code:
 
awk -F":" '/newjob/{v=$2"~"};/command/{a=$2};{print v""a;v="";a=""}' filename

I am just struggling to get those record in one line as per requirements
# 4  
Old 05-15-2013
This awk gives what OP request
Code:
awk -F"[: ]+" '/newjob/&&f {printf "\n%s",$2} /newjob/&&!f {printf $2;f=1} /command/ {print "~"$3;f=0}' file
abc~a=b+c
bbc~c=r+v
ddc
kkc~c=l+g
mdc
ldc
kjc~u=dl+g
lkdc
lksdc~o=udl+g


Last edited by Jotne; 05-15-2013 at 05:58 AM..
This User Gave Thanks to Jotne For This Post:
# 5  
Old 05-15-2013
Just that tild ~ missing where command is not found Smilie
# 6  
Old 05-15-2013
Here is a slightly different awk script that produces the requested heading line and the trailing tildes when there is no command associated with the job:
Code:
awk -F ':[ \t]*' '
BEGIN { OFS="~"
        print "NEWJOB", "COMMAND"
}
/^newjob/ {
        if(j != "") {
                print j, c
                c = ""
        }
        j = $2
}
/^command/ {
        c = $2
}
END {   if(j != "") print j, c
}' Input > output

which places
Code:
NEWJOB~COMMAND
abc~a=b+c
bbc~c=r+v
ddc~
kkc~c=l+g
mdc~
ldc~
kjc~u=dl+g
lkdc~
lksdc~o=udl+g

in the output file.
# 7  
Old 05-15-2013
Now all should be included, header too Smilie
Code:
awk -F"[: ]+" 'BEGIN {print "NEWJOB~COMMAND"} /newjob/&&f {printf "~\n%s",$2} /newjob/&&!f {printf $2;f=1} /command/ {print "~"$3;f=0}' file
NEWJOB~COMMAND
abc~a=b+c
bbc~c=r+v
ddc~
kkc~c=l+g
mdc~
ldc~
kjc~u=dl+g
lkdc~
lksdc~o=udl+g

Easy to read version
Code:
awk -F"[: ]+" '
BEGIN {print "NEWJOB~COMMAND"} 
/newjob/&&f {printf "~\n%s",$2}
/newjob/&&!f {printf $2;f=1} 
/command/ {print "~"$3;f=0}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. UNIX for Dummies Questions & Answers

Print Matches to New Columns

Hi all, I have a problem that I'm struggling to resolve. I have two files that look like this: File 1 654654654 3 987987987 2 321321321 1 File 2 14NS0064 654654654 14NS0054 654654654 14NS0032 654654654 14NS0090 987987987 14NS0093 987987987 14NS0056 321321321 As you may notice,... (2 Replies)
Discussion started by: winkleman
2 Replies

3. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

Match two columns from two files and print output

Hello, I have two files which are of the following format File 1 which has two columns Protein_ID Substitution NP_997239 T53R NP_060668 V267M NP_058515 P856A NP_001206 T55M NP_006601 D371Y ... (2 Replies)
Discussion started by: nans
2 Replies

5. Shell Programming and Scripting

match two key columns in two files and print output (awk)

I have two files... file1 and file2. Where columns 1 and 2 of file1 match columns 1 and 2 of file2 I want to create a new file that is all file1 + columns 3 and 4 of file2 :b: Many thanks if you know how to do this.... :b: file1 31-101 106 0 92 31-101 106 29 ... (2 Replies)
Discussion started by: pelhabuan
2 Replies

6. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

7. UNIX for Advanced & Expert Users

Print rows into columns

Hi, I required to print all rows into columns(some comma separated or else) till a pattern match found. Using Solaris UNIX scripting my input is like abc def ghi jkl mno END pqr stu vwx yz 123 END ... ... (7 Replies)
Discussion started by: vikash.rastogi
7 Replies

8. Shell Programming and Scripting

How to print first 'n' columns?

Hello All, I have a file of 1000 columns. I need to print only the first 100 columns in to a new file. I have tried with the following code awk -F '{ for(i=0; i<=100; i++) {printf $i}' inputfile > outputfile But this is not working, I need the output file also of the same format of the... (4 Replies)
Discussion started by: Fredrick
4 Replies

9. Shell Programming and Scripting

want to print output if u have rows and columns

U have a file File 1 0.3 0.2 0.4 0.3 0.8 0.11 0.22 0.4 0.23 0.45 0.56 0.78 0.98 0.11 0.32 0.2 0.4 0.45 0.54 0.2 0.33 0.44 0.21 0.22 0.98 0.8 0.2 0.34 0.54 0.98 0.12 0.1 0.22 0.32 0.34 0.89 0.22 File 2 rows columns 3 1 2 3 4 2 5 ... (8 Replies)
Discussion started by: cdfd123
8 Replies

10. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies
Login or Register to Ask a Question