Monday, August 22, 2016

[Linux, shell script] read a line or column from a file

< Read line by line from a file >

 filename.txt
-----------------------
aaa 111 AAA
bbb 222 BBB
ccc 333 CCC
-----------------------


Shell script
-----------------------#!/bin/bash

while read line
do

    echo "-> ${line}"

done < filename.txt
-----------------------

Result
------------------------> aaa 111 AAA
-> bbb 222 BBB
-> ccc 333 CCC
-----------------------








 

< Read column by column from a file >


filename.txt
-----------------------
aaa 111 AAA
bbb 222 BBB
ccc 333 CCC
-----------------------


Shell script
-----------------------#!/bin/bash

while read col1 col2 col3
do

    echo "${col1}/${col2}/${col3}"

done < filename.txt
-----------------------
 


Result
-----------------------aaa/111/AAA
bbb/222/BBB
ccc/333/CCC
-----------------------

No comments:

Post a Comment