简体   繁体   中英

read line and remove newline character using shell script

I have shell script which generate sql queries based on a values in text file. My text file has values as follows (line by line)在此处输入图像描述

my shell script is this.

#!/bin/sh
NAME="tableNames.txt"
COLUMNA="ca"
COLUMNB="cb"
cat $NAME | while read LINE

do
echo "CREATE TABLE \"$LINE\" (
        forward_speed double precision,
    backward_speed double precision
      );"
done

LINE variable get the value from textfile correctly but it has newline character how do i remove new line character.

You probably generated the text file on a windows machine or some other setting with dos line endings. You can fix that by either

  • converting the file to unix line endings with dos2unix
  • deleting '\r' characters: cat $FILE | tr -d '\r' | while read LINE ... cat $FILE | tr -d '\r' | while read LINE ...
  • use a utility like awk to grab the first field: cat $FILE | awk '{print $1}' | while read LINE ... cat $FILE | awk '{print $1}' | while read LINE ...

如果tabeNames.txt来自 Windows,你应该做一个dos2unix过滤器。

I had this problem working with output from a Busybox docker container on Fedora.

I was using the output from docker run to do further processing, but could'nt get it to work for multiple hours.

I solved my problem by piping the output to a file

...
res=$(docker run -it ....)
echo $res > tmp.txt
dos2unix tmp.txt
cat tmp.txt | while read line;
do
...

Hope this helps others solve similar issues with container output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM