How to take a single record in a file and display it vertically with the count of field before the actual field is displayed

StackOverflow https://stackoverflow.com/questions/20735545

  •  20-09-2022
  •  | 
  •  

質問

I have a file with a single line in it that has a record that is deliminted by semi-colons. So far I have figured out that I can use tr by issuing:

tr ';' '\n' < t

However since the record has 140 fields, I'd like to be able to show the field count when displaying such as the following:

1 23
2 324234
3 AAA
.
.
140 Blah

Help is greatly appreciated!

役に立ちましたか?

解決 2

You could just run it through cat -n.

tr \; '\n' < t | cat -n

Since this is tagged awk, you could do it that way, too; it's just a little wordier:

awk -F\; '{for (i=1;i<=NF;++i) { print i" "$i }}'

他のヒント

tr \; '\n' <t|nl

or

awk -v RS=';'  '$1=++i" "$1' file

test:

kent$  echo "a;b;c;d"|awk -v RS=';'  '$1=++i" "$1'     
1 a
2 b
3 c
4 d

In shell you can use IFS to specify a field separator like so,

IFS=";"
i=0
for s in $(<file)
do
  ((i++)) 
  echo $i $s
done
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top