0

I have come across a scenario where I need to print text part of the same line when using the cat command or sed command for example.

I have a file with 5 line in it showing

Tom

Seniorsupport

5

U

My aim is to print specific text in front of each line giving it another column being more specific

OUTPUT I WANT:

Grantee/Username:   Tom

Role name:          Seniorsupport

Priority:           5

Usertype:           U

There are many ways this can be done, but I would like a quick and easy way

Christopher Karsten
  • 387
  • 1
  • 2
  • 12

3 Answers3

0

One way I have decided to go about this is using the awk command.

cat filename | awk '{print "Grantee/Username:   " $1}' | sed -n '1p'

The above command will cat the file and print the text using the awk function printing only line 1 within the file using the sed command

riteshtch
  • 8,629
  • 4
  • 25
  • 38
Christopher Karsten
  • 387
  • 1
  • 2
  • 12
0

You can have the titles (column 1 ) stay in file1 and the actual values in file2 and then you can paste them side by side using paste command like this:

$ cat file1
Grantee/Username
Role name
Priority
Usertype
$ cat file2
Tom
Seniorsupport
5
U
$ paste <(awk '{printf "%-19s\n", $0 ":"}' file1) file2
Grantee/Username:   Tom
Role name:          Seniorsupport
Priority:           5
Usertype:           U
riteshtch
  • 8,629
  • 4
  • 25
  • 38
0

Write the headers in another file ("keyfile"). Combine data and keys with awk. Using the tricks explained in What is "NR==FNR" in awk? you can write

awk 'NR==FNR {a[FNR]=$0; next} {printf("%-20s%s\n", a[FNR] ":", $0) }' keyfile datafile
Community
  • 1
  • 1
Walter A
  • 19,067
  • 2
  • 23
  • 43