Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it easy/possible to display columns of text which is mapped from digits? 1

Status
Not open for further replies.

marcq

Technical User
Aug 18, 2011
34
PL
Hi.
I'd like to write some script in shell that will display final report but will map digits to Text.
Let me explain that:
Assume I have file which is result of some operation like example:

YYMMDDHHMMSS 5 10 5 2
YYMMDDHHMMSS 6 9 4 2

Now.
You see there are 5 columns.
In colums 2-5 there is possible different digits which can have defined description in text.
Like column 2:
5 -> Text
6 -> Text1
Column 3:
10 -> Text2
9 -> Text 9
etc....

How I can than display it to have like that:
YYMMDDHHMMSS Text Text2 ... ...
YYMMDDHHMMSS Text1 Text9 ... ...
 
Hi

Definitely possible. Probably the easiest is with Awk :
Code:
[blue]master #[/blue] cat data.txt 
YYMMDDHHMMSS 5 10 5 2
YYMMDDHHMMSS 6 9 4 2

[blue]master #[/blue] cat description.txt 
2 5 -> Text
2 6 -> Text1
3 10 -> Text2
3 9 -> Text 9

[blue]master #[/blue] awk -F ' *-> *' '[blue]FNR[/blue][teal]==[/teal][blue]NR[/blue][teal]{[/teal]d[teal][[/teal][navy]$1[/navy][teal]]=[/teal][navy]$2[/navy][teal];[/teal][COLOR=chocolate]next[/color][teal]}[/teal][blue]FNR[/blue][teal]==[/teal][purple]1[/purple][teal]{[/teal][blue]FS[/blue][teal]=[/teal][green][i]" "[/i][/green][teal];[/teal][navy]$0[/navy][teal]=[/teal][navy]$0[/navy][teal]}{[/teal][b]for[/b][teal]([/teal]i[teal]=[/teal][purple]2[/purple][teal];[/teal]i[teal]<=[/teal][blue]NF[/blue][teal];[/teal]i[teal]++)[/teal][b]if[/b][teal]([/teal]i[green][i]" "[/i][/green][navy]$i[/navy] [teal]in[/teal] d[teal])[/teal][navy]$i[/navy][teal]=[/teal]d[teal][[/teal]i[green][i]" "[/i][/green][navy]$i[/navy][teal]][/teal][teal]}[/teal][purple]1[/purple]' description.txt data.txt 
YYMMDDHHMMSS Text Text2 5 2
YYMMDDHHMMSS Text1 Text 9 4 2
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Of course, if your descriptions are stored in other format then the one I imagined, some modifications will be needed.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top