Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read line and remove newline character using shell script

Tags:

linux

shell

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) enter image description here

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.

like image 442
smk Avatar asked Oct 25 '13 07:10

smk


2 Answers

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 ...
  • use a utility like awk to grab the first field: cat $FILE | awk '{print $1}' | while read LINE ...
like image 157
SheetJS Avatar answered Sep 19 '22 00:09

SheetJS


If tabeNames.txt is from Windows you should do a dos2unix filter.

like image 25
PeterMmm Avatar answered Sep 18 '22 00:09

PeterMmm