0

I have a file as follows. file1.csv

H,2 A:B,pq
D,34 C:B,wq
D,64 F:B,rq
D,6 R:B,tq

I want to format 2nd a column as follows

H,02 0A:0B,pq
D,34 0C:0B,wq
D,64 0F:0B,rq
D,06 0R:0B,tq

I am able to separate the column and format it but cannot merge it I use following command

formated_nums =`awk -F"," '{print $2}' file1.csv | awk '{print $1}' | awk '{if(length($1)!=2){$1="0"$1}}1'`
formated_letters = `awk -F"," '{print $2}' file1.csv | awk '{print $2}' | awk -F":" '{if(length($1)!=2){$1="0"$1}; if(length($2)!=2){$2="0"$2}}1'| awk '{print $1":"$2}'`

Now I want to merge formated_nums and formated_letters with a space in between

I tried echo "${formated_nums} ${formated_letters}" but it takes variables as rows and appends the whole thing as a row

2
  • use printf instead of print that add a new line at the end. See other reply to avoid piped awk. Awk is powerfull an can easily handle such double trnasformation, it is made for. Commented May 17, 2017 at 7:15
  • In a comment under a now deleted answer you said "it won't work for an input like H,2 0A:B,pq". Obviously all we have to go on is what you tell us so if you need to handle input like that (or anything else) then edit your question to include that in your sample input/output. And please do it soon before anyone else wastes time offering you solutions based on sample input that doesn't reflect your real input and so cannot work. Commented May 17, 2017 at 19:44

3 Answers 3

1

The simplest I found in awk is to use another separation including space and ':' and reformat the final layout. The only real tricky part is the number that need sometimes to add a 0 in front but it's trivial in formating because number are never bigger than 2 digit (here)

awk -F '[[:blank:],:]' '{printf("%s,%02d 0%s:0%s,%s", $1, $2, $3, $4, $5)}' YourFile

Assuming your data are in the same format (no bigger latest field with space or other "separator" inside)

Sign up to request clarification or add additional context in comments.

Comments

0

An alternative awk solution based on gnu awk :

awk -F"[, :]" '{sub($2,sprintf("%02d",$2));sub($3,"0" $3);sub($4,"0" $4)}1' file1

H,02 0A:0B,pq
D,34 0C:0B,wq
D,64 0F:0B,rq
D,06 0R:0B,tq

1 Comment

There's nothing gawk-specific about that, it'll behave the same in any awk. It just won't work if the input contains regexp or backreference metacharacters.
0

It sounds like this is what you're really looking for:

$ awk '
    BEGIN { FS=OFS=","; p=2 }
    { split($2,t,/[ :]/); for (i in t) {n=length(t[i]); t[i] = (n<p ? sprintf("%0*s",p-n,0) : "") t[i]; $2=t[1]" "t[2]":"t[3]} }
    1
' file
H,02 0A:0B,pq
D,34 0C:0B,wq
D,64 0F:0B,rq
D,06 0R:0B,tq

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.