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.
LVL 1
hi4pplAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.