[Bash] While read, pass the contents of the file to variables for each column

Assuming you have the following SSV file

cat file | while read line; do
 echo "$line"
done

1 hoge foo piyo
2 hoge foo piyo
3 hoge foo piyo

↑ Assign this content to a variable for each column ** and process **.

#Try to output the 1st and 2nd columns
cat file | while read col1 col2 col3 col4; do
 echo "$col1" "$col2"
done

1 hoge
2 hoge
3 hoge

With the above, isn't it necessary to define col3 and col4? In such a case, store it in a variable and throw it away.


cat file | while read col1 col2 x; do
 echo "$col1" "$col2"
done

1 hoge
2 hoge
3 hoge

#Take a look at the contents of x
cat file | while read col1 col2 x; do
 echo "$col1" "$col2" "x:$x"
done

1 hoge x:foo piyo
2 hoge x:foo piyo
3 hoge x:foo piyo

#If you want only the 1st and 3rd rows
cat file | while read col1 x col3 y; do
 echo "$col1" "$col3" "x:$x y:$y"
done

1 foo x:hoge y:piyo
2 foo x:hoge y:piyo
3 foo x:hoge y:piyo


Recommended Posts

[Bash] While read, pass the contents of the file to variables for each column
Template of python script to read the contents of the file
Output the specified table of Oracle database in Python to Excel for each file
How to see the contents of the Jupyter notebook ipynb file
[EC2] How to install chrome and the contents of each command
Read all the contents of proc / [pid] ~ From setgroups to wchan ~
Read all the contents of proc / [pid] ~ From cwd to loginuid ~
Read all the contents of proc / [pid] ~ From map_files to numa_maps ~
Read all the contents of proc / [pid] ~ From oom_adj to sessionid ~
Read all the contents of proc / [pid] ~ from attr to cpuset ~
Read all the contents of proc / [pid]
Various ways to read the last line of a csv file in Python
I want to add silence to the beginning of a wav file for 1 second
How to find the correlation for categorical variables
Convert financial information of all listed companies for the past 5 years to CSV file
Read the xml file by referring to the Python tutorial
Try to get the contents of Word with Golang
Set the specified column of QTableWidget to ReadOnly StyledItemDelegate
Date the name of the decomposed PDF for each page
Also read the contents of arch / arm / kernel / swp_emulate.c
I searched for the contents of CloudWatch Logs Agent
[Ubuntu] How to delete the entire contents of a directory
How to switch the configuration file to be read by Python
Django returns the contents of the file as an HTTP response
Check the operation of Python for .NET in each environment
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
Consideration for Python decorators of the type that passes variables
Settings to debug the contents of the library with VS Code
(Note) How to pass the path of your own module
In omegaconf, let's pass the direct parameter file to the function
Get the number of occurrences for each element in the list
Google search for the last line of the file in Python
How to connect the contents of a list into a string
I tried to summarize the contents of each package saved by Python pip in one line