0

Am trying to create files using variable in a bash script:

new_name=`sed -n "/:20:/p" $file | awk -F: '{print $3}'`

When I echo the variable $new_name, the result is correct: TRS105489299

When I use the touch command to create the file, the result looks like the below: 'TRS105489299'$'\r'

How can create the file with the name TRS105489299?

$ sed -n "/:20:/p" ~/1.txt  | hexdump.exe -C
00000000  3a 32 30 3a 54 52 53 31  30 35 34 38 39 32 39 39  |:20:TRS105489299|
00000010  0d 0a                                             |..|
00000012

$ sed -n "/:20:/p" ~/1.txt | awk -F: '{print $3}' | hexdump.exe -C
00000000  54 52 53 31 30 35 34 38  39 32 39 39 0d 0a        |TRS105489299..|
0000000e

Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
mhm
  • 1
  • 1
  • Please take a look at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting). – Cyrus Jun 19 '22 at 19:52
  • Please add output of `sed -n "/:20:/p" $file | hexdump -C` to your question (no comment). – Cyrus Jun 19 '22 at 19:53
  • It looks like your file (the one referred to by `$file`) is in DOS/Windows format, which causes a lot of confusion for unix tools. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings). – Gordon Davisson Jun 19 '22 at 20:01
  • `$'\r'` is a nonprintable character, so it doesn't show up in `echo` output. Use `set -x` to enable trace logging. – Charles Duffy Jun 19 '22 at 20:37
  • Am using cygwin64, wouldn't I face the same issue if am on Linux? – mhm Jun 19 '22 at 20:37
  • You would face the same issue on Linux _only if you had files in DOS text format_, which people on Linux generally don't, because, well, Linux-y tools save in UNIX text format by default. But this is all about defaults; you can tell any decent Windows text editor to save in UNIX format and the inverse. – Charles Duffy Jun 19 '22 at 20:37
  • 1
    See the `0d0a` in your hexdump -- the `0d` is a CR, aka `\r`; the `0a` is a LF, aka `\n`. If your file were saved in UNIX format only the `0a` would be there, because Windows uses CRLF sequences whereas UNIX uses only LF. – Charles Duffy Jun 19 '22 at 20:40

0 Answers0