Shell script handles text files && space transformation lines line by row

Source: Internet
Author: User

Reference: http://www.cnblogs.com/dwdxdy/archive/2012/07/25/2608816.html

Often to the stylistic document for line-by-row processing, in the shell how to obtain each row of data, and then processing the row of data, and finally read the next line of data, circular processing. There are several solutions:

1. Complete with the Read command.

The read command receives the input of standard input, or other file descriptors, and when entered, the Read command puts the data into a standard variable.

When reading a file with read, each call to the Read command reads the "one line" text in the file.

When a file has no readable rows, the read command exits in a non-0 state.

1 Cat Data.dat | While read line
2 does
3     echo "File:${line}"
4 done
5 
6 while read line
7 do 
8     echo " File:${line} "
9 done < Data.dat

2. Use awk command to complete

Awk is an excellent text processing tool that provides extremely powerful functionality.

Using awk, you can read each row of data in a file, do some processing on each row of data, and process each column of data in each row of data separately.

1 Cat Data.dat | awk ' {print $} '
2 cat Data.dat | awk ' for (i=2;i<nf;i++) {printf $i} printf ' \ n '} '

The 1th line of code outputs each row of data in Data.dat, and the 2nd code outputs data from the 2nd column in each row.

It is convenient to use the awk command if the simple data or text files are read and displayed in rows.


3. Use the for var in file command to complete

For Var in file indicates that variable var loops through the value in file. The value delimiter is determined by $ifs.

1 for line in $ (cat data.dat)
2 do 
3     echo "File:${line}"
4 did
5 6 for line in 
' Cat Data.dat ' 
  
   7 do 
8     echo ' File:${line} '
9 done
  

If the input text has no spaces in each row, line takes the value in the input text by the newline character delimiter.

If the input text includes spaces or tabs, it is not a newline read, line in the input text by the space Separator or tab or line traits classes ring value.

You can achieve line-by-row reading by setting the IFS to line breaks.

Demo

Suppose you now need to read the following file Rollback_config.txt:

Rollback_services:upserv Checkserv

rollback_version:v1.1

Use for line in ' Cat rollback_config.txt '; Do echo "${line}"; The results of the done reading will be:

Rollback_services:upserv

Checkserv

rollback_version:v1.1

Obviously not what we want.


Workaround:

Ifs_old= $IFS
ifs=$ ' \ n '
For line in ' Cat rollback_config ';d o
echo "$line"
Done
ifs= $IFS _old

So it's OK.




the default value for IFS is: blank (includes: spaces, tabs, line breaks).


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.