AWK command to cut the desired header columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK command to cut the desired header columns
# 1  
Old 08-21-2012
Question AWK command to cut the desired header columns

Hi Friends,

I have a file1

Quote:
Depid|Loc|City|PHNO|Trans|Acco
1|trr|rft|2344|454545|45344|344554
2|efr|wer|454|543534|34534|5242
i want to retrieve only the fields which have DEP,CITY,TRANS as headers in other file.

Output:
Quote:
DEP|CITY|TRANS
1|2344|45344
2|454|34534
I want to give the input as DEP,CITY,TRANS column names to get the output.

i used cut command .. but if i have 300 fileds it is more difficult to search for the field names. i want to use AWk commad to get the output

plz help.
# 2  
Old 08-21-2012
Looks very much like home work... No?
# 3  
Old 08-21-2012
Try like..
Code:
awk -F\| '{ print $1"|"$3"|"$5}' test1.txt

# 4  
Old 08-21-2012
Code:
awk -F\| -vcols='Dep,City,Trans' '
NR==1 {
for(i=1;i<=NF;i++)
 if(index(cols,$i))
 {
  c[i]=1
  t=(t)?(t FS $i):$i
 }
print t;next
}
{
t=""
for(i=1;i<=NF;i++)
 if (i in c)
  t=(t)?(t FS $i):$i
print t
}' file

# 5  
Old 08-21-2012
@vbe: Thanks for the reply .no its not hme work, i tried with awk and cut but the file which i am getting is not static . so thougth of taking help .

@bmk : Thanks for the reply. yes i tried with same command. but file which i get is dymanic the field position may change Smilie

@Elixir : Thanks for the reply. i will execute and try this now .
@
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split command to get the desired result

Dear all, I am using the awk 'split' command to get the particular value. FILE=InputFile_009_0.txt Temp=$(echo $FILE | awk '{split($FILE, a, "e_"); print a}') I would like to have the Temp take the value as : _009_0 ... (4 Replies)
Discussion started by: emily
4 Replies

2. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

3. Shell Programming and Scripting

Help with cut or awk command

i am trying to cut the 4th field from the file. sample file a1,a2,"a,3",a4,a5,a6 b1,"b,2",b3,b4,b5,b6 c1,"c,2","c,3",c4,c5,c6 i need the output in the format of a1,a2,"a,3",a5,a6 b1,"b,2",b3,b5,b6 c1,"c,2","c,3",c5,c6 i tried using cut -d, -f1-4,6- but i am getting the output as ... (10 Replies)
Discussion started by: ATWC
10 Replies

4. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

5. Shell Programming and Scripting

awk and cut command

Hi, I have to display the value 16 present in "lcpu=16" which is the output of a command # vmstat System configuration: lcpu=16 mem=4096MB I used as # hdtype=`vmstat | grep "lcpu" | awk -F "=" '{print $2}'` # echo $hdtype 16 mem But I need to display only 16.. Am doing... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. UNIX for Dummies Questions & Answers

Regarding cut or awk command

Hi, I need a help with cut/awk command. I need to get certain values from a string provided. For example: i have a config file with below mentioned details oracle="user=name"/"pass/word"@databasename. I have used a command var1=`grep -w oracle sample.cfg | cut -d"=" -f2 | cut -d"/" -f1`. ... (10 Replies)
Discussion started by: kumars2102
10 Replies

7. Shell Programming and Scripting

extract/cut desired output from a line

Hi all , my script is sending output to a file , output is in below format :- i want to cut , celltadm@avm-siapp01 from this output line . Starting and ending words can vary , like it can be any name as below - so there will be only one "@" and i want to cut word just before and after... (8 Replies)
Discussion started by: deepakiniimt
8 Replies

8. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

9. Shell Programming and Scripting

awk command for simple join command but based on 2 columns

input1 a_a a/a 10 100 a1 a_a 20 200 b1 b_b 30 300 input2 a_a a/a xxx yyy a1 a1 lll ppp b1 b_b kkk ooo output a_a a/a 10 100 xxx yyy (2 Replies)
Discussion started by: ruby_sgp
2 Replies

10. UNIX for Dummies Questions & Answers

cut and paste columns using awk

Hi, Let's say that I have a file called table, I know that if I need to see a the second column for exampls I use: awk ' {print $2}' table.txt Is there anyway to use awk to actually cut a column and put it somewhere else in the table?:confused: (8 Replies)
Discussion started by: cosmologist
8 Replies
Login or Register to Ask a Question