Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

using awk to add header and separotor

Hi,

I have following awk in linux that check all files with .out and select those files and give me out put.

awk --field-separator=";" '{ print $2,$11 }' *.csv> all.txt

what i'm looking for is:

account|name
243343|john
234343|cena
                                 

but I want to add header and separator for each field which i'm not succesfull as of yet, please suggest the same for awk or alternative.
0
hi4ppl
Asked:
hi4ppl
  • 12
  • 11
1 Solution
 
ozoCommented:
account|name
243343|john
234343|cena
Is that supposed to be the input or the output?
If it is the input, what would you want the corresponding output to be?
If it is the desired output, what was the corresponding input?
0
 
hi4pplAuthor Commented:
Hi,

Thanks for replay, that is out put that i'm looking for, the input file have many fields I only select those two fields,
what the input looks like is bellow:

account;name;fathername;country;city;....
243343;john;cena;USA;PA;.....
23434;|cena;john;france;AM;....

(...) represent that there more field around 25 but that is how it looks like.

regards
0
 
ozoCommented:
awk -F";[|]*" -v OFS="|" '{ print $1,$2 }' <<END
account;name;fathername;country;city;....
243343;john;cena;USA;PA;.....
23434;|cena;john;france;AM;....
END
Should produce your desired output
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hi4pplAuthor Commented:
Hi,

thanks but in example i'm pushing files as *.cv in your example it put all records their, how can I pass the file name?

regards
0
 
ozoCommented:
awk -F";[|]*" -v OFS="|" '{ print $1,$2 }' *.cv
0
 
hi4pplAuthor Commented:
hi, it does not work it says syntax error:

awk : testfile.csv
awk:              ^ syntax error 

Open in new window

0
 
ozoCommented:
Can you paste the command you used?
0
 
hi4pplAuthor Commented:
hi here it's


awk -F";[|]*" -v OFS="|"'{ print $2,$11,$15,$46,$48,$62 }' *.csv

Open in new window

0
 
ozoCommented:
There should be a space between OFS="|" and '{ print $2,$11,$15,$46,$48,$62 }'

But  print $2,$11
would skip the
account
243343
in
account;name;fathername;country;city;....
243343;john;cena;USA;PA;.....
0
 
hi4pplAuthor Commented:
Hi, thanks yeah I know that that was just exmaple it works but what it does not work is it does not print out the header information, it only add |, I wanted to print the header information also.

regards
0
 
ozoCommented:
Can you post an example of your desired output?
0
 
hi4pplAuthor Commented:
hi, this is what it does now:

243343|john
234343|cena

what I want is

account|name
243343|john
234343|cena

regards
omer
0
 
ozoCommented:
If your input looks like

1;account;3;4;5;6;7;8;9;10;name;fathername;country;city;....
1;243343;3;4;5;6;7;8;9;10;john;cena;USA;PA;.....
1;234343;3;4;5;6;7;8;9;10;cena;

then your output would look like

account|name|....|||
243343|john|.....|||
234343|cena||||
0
 
hi4pplAuthor Commented:
Hi, but it does not give me that result my input looks like this :

account;name;fathername;country;city;....
243343;john;cena;USA;PA;.....
23434;|cena;john;france;AM;....

but it does not give me the header  like

account|name|....|||
243343|john|.....|||
234343|cena||||
0
 
ozoCommented:
Running
awk -F";[|]*" -v OFS="|" '{ print $2,$11,$15,$46,$48,$62 }' <<END
account;name;fathername;country;city;....
243343;john;cena;USA;PA;.....
23434;|cena;john;france;AM;....
END

would get
name|||||
john|||||
cena|||||

If you want

account|name|....|||
243343|john|.....|||
234343|cena||||

then you can run

awk -F";[|]*" -v OFS="|" '{ print $1,$2,$6,$7,$8,$9}' <<END
account;name;fathername;country;city;....
243343;john;cena;USA;PA;.....
234343;|cena;john;france;AM;....
END
0
 
hi4pplAuthor Commented:
hi,
when i run your sample alone it works but when I run this on the file *.csv it gives me result without the header,
one how it make difference that you printed $1 and it print the column names but when I try in my file it print the column 1 not the header itself.

regards
0
 
ozoCommented:
Can you post a sample of what *.csv actually looks like?
0
 
hi4pplAuthor Commented:
well it's big file around 3GB but it looks like the sample I provided , at the top of the row there is all header which is around 120, then after that it's all data.

here is the sample again

account;name;fathername;country;city
243343;john;cena;USA;PA
23434;|cena;john;france;AM

Open in new window


regards
0
 
hi4pplAuthor Commented:
hi sorry my bad I had one more condition here:

when I run it

awk -F";[|]*" -v OFS="|" '{ print $2,$11 }' *.csv

Open in new window


it gives me header also but when I run

awk -F";[|]*" -v OFS="|" '$11=="Active"{ print $2,$11 }' *.csv

Open in new window


a condition to output only records that $11 have Active, so is there a way to have header also and that condition will work as well?

sorry for confusion I just notice it now
0
 
ozoCommented:
if
cat *.csv
prints
account;name;fathername;country;city
243343;john;cena;USA;PA
23434;|cena;john;france;AM

Open in new window

then
awk -F";[|]*" -v OFS="|" '{ print $2,$11,$15,$46,$48,$62 }' *.csv
would print
name|||||
john|||||
cena|||||

Open in new window

0
 
ozoCommented:
What does $11 look like when there is a header?  Or how can you tell which non"Active" line contains a header?
If it $11 is "name" in a header, then you might use
'$11=="Active"||$11=="Name"{ print $2,$11 }'
if the first line is always a header, then you might use
'$11=="Active"||NR==1{ print $2,$11 }'
0
 
hi4pplAuthor Commented:
Thanks it works :D
0
 
hi4pplAuthor Commented:
Hi,

this is might be out of this question but, can I customize the header name? in other word can I give name to each variable $4 to something excluding the header they have i.e if the header in main file is AccountID can i change it to Account only?
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now