Editing a text format using a shell script

I am trying to make a script for editing text. In this case, I have a text file called text.csv that states:

first;48548a;48954a,48594B second;58757a;5875b third;58756a;58576b;5867d;56894d;45864a 

I want to make a text format as follows:

 first;48548a first;48954a first;48594B second;58757a second;5875b third;58756a third;58576b third;5867d third;56894d third;45864a 

What should I use to make this happen?

+4
source share
4 answers

I would do it in awk.

Assuming your first line should have ; instead of:

 $ awk -F\; '{for(n=2; n<=NF; n++) { printf("%s;%s\n",$1,$n); }}' input.txt 

Unverified.

+6
source

Here is a clean bash solution that handles both , and ; .

 while IFS=';,' read -a data; do id="${data[0]}" data=("${data[@]:1}") for item in "${data[@]}"; do printf '%s;%s\n' "$id" "$item" done done < input.txt 

UPDATED - an alternative printing method based on the chepner suggestion:

 while IFS=';,' read -a data; do id="${data[0]}" data=("${data[@]:1}") printf "$id;%s\n" "${data[@]}" done < input.txt 
+2
source

awk -v FS=';' -v OFS=';' '{for (i = 2; i <= NF; ++i) { print $1, $i }}' awk -v FS=';' -v OFS=';' '{for (i = 2; i <= NF; ++i) { print $1, $i }}' Explanation: awk implicitly splits data into records (by default, separeted by newline, i.e. String == record) , which are then divided into numbered fields by the specified field separator ( FS for the input field separator and OFS for the output separator). For each record, this script prints the first field (which is the name of the record) along with the i-th field and exactly what you need.

+1
source
 while IFS=';,' read -a data; do id="${data[0]}" data=("${data[@]:1}") printf "$id;%s\n" "${data[@]}" done < input.txt 

or

 awk -v FS=';' -v OFS=';' '{for (i = 2; i <= NF; ++i) { print $1, $i }}' 

AND

 $ awk -F\; '{for(n=2; n<=NF; n++) { printf("%s;%s\n",$1,$n); }}' input.txt 

Thank you all for your suggestions: d. It really gives me new knowledge.

+1
source

All Articles