user3326101
user3326101

Reputation: 51

Capture specific columns from delimited file

I am new to shell scripting and i need to create a shell script with the below condition.

The shell script should read the Inputfile.txt each line and take the first and sixth column in the file and create a new outputfile.txt

Inputfile.txt

123 | xyz | abc | abc | abc | 567

321 | xyz | abc | abc | abc | 765

Outputfile.txt

123 | 567

321 | 765

Upvotes: 2

Views: 1762

Answers (4)

mjuarez
mjuarez

Reputation: 16834

Using a simple cut command should be enough:

cut -d "|" -f 1,6

Upvotes: 6

MLSC
MLSC

Reputation: 5972

you can use awk as well:

awk  '{print $1, $10, $11}' input.txt

(OR)

awk -F'|' '{print $1 "|" $6  $11}' input.txt

(OR)

awk '{print $1, $11}' input.txt

(OR)

You can simply use cut command:

cut -d"|" -f 1,6

(OR)

Using sed:

sed 's/|.*|/|/' input.txt

Also with grep...But I am not that familiar with it yet :)

Also: @anubhava told me:

awk -F '[| ]+' '{print $1, $NF}' OFS=' | ' input.txt

(OR)

while IFS=' | ' read -a arr
    echo "${arr[0]} | ${arr[@]:(-1)}"
done < input.txt

Upvotes: 0

John1024
John1024

Reputation: 113844

To capture the first and sixth items, and then print them out separated by vertical bars:

$ awk -F'|' '{print $1 "|" $6}' inputfile.txt 
123 | 567 321

The -F'|' tells awk to use the vertical bar as the field separator on the input file.

To save the above output in outputfile.txt, use redirection:

$ awk -F'|' '{print $1 "|" $6}' inputfile.txt >outputfile.txt

Alternatively, the same result can be achieved by setting awk's output field separator (OFS) to a vertical bar:

$ awk -F'|' 'BEGIN{OFS="|"} {print $1,$6}' inputfile.txt >outputfile.txt

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77105

If you don't want to use any tools then here is a way to get first and last elements in bash:

$ cat inputfile.txt 
123 | xyz | abc | abc | abc | 567
321 | xyz | abc | abc | abc | 765

$ while IFS="|" read -ra line; do 
    echo "${line[0]}|${line[${#line[@]}-1]}"
done < inputfile.txt 
123 | 567
321 | 765

You can use literal subscripts too, so instead of ${line[${#line[@]}-1]} you can do ${line[5]}.

Upvotes: 1

Related Questions