How do I preserve leading whitespaces with echo on a shell script?

heisenbergman

I have a source file that is a combination of multiple files that have been merged together. My script is supposed to separate them into the original individual files.

Whenever I encounter a line that starts with "FILENM", that means that it's the start of the next file.

All of the detail lines in the files are fixed width; so, I'm currently encountering a problem where a line that starts with leading whitespaces is truncated when it's not supposed to be truncated.

How do I enhance this script to retain the leading whitespaces?

while read line         
do         
    lineType=`echo $line | cut -c1-6`
    if [ "$lineType" == "FILENM" ]; then
       fileName=`echo $line | cut -c7-`
    else
       echo "$line" >> $filePath/$fileName
    fi   
done <$filePath/sourcefile
petersohn

The leading spaces are removed because read splits the input into words. To counter this, set the IFS variable to empty string. Like this:

OLD_IFS="$IFS"
IFS=
while read line         
do
    ...
done <$filePath/sourcefile
IFS="$OLD_IFS"

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I allow whitespaces in this regex?

분류에서Dev

How do I Successfully Echo a For Loop Into a Batch Script?

분류에서Dev

How do I replace a whitespace with any number of whitespaces?

분류에서Dev

How do I switch desktop session users from a shell script?

분류에서Dev

how do i create a shell script with multiple choice menu variables?

분류에서Dev

How do I pass a variable from a shell script to my .muttrc?

분류에서Dev

How can I do basic calculations in a shell script?

분류에서Dev

How do I bring HEREDOC text into a shell script variable?

분류에서Dev

How do I run a windows executable in linux shell script?

분류에서Dev

How do I prevent a script from terminating when the shell exits?

분류에서Dev

How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

분류에서Dev

How do I preserve the current viewport when removing a div?

분류에서Dev

How to do float operation in Shell script?

분류에서Dev

How to do string comparison properly in shell script?

분류에서Dev

How do I find the latest date folder in a directory and then construct the command in a shell script?

분류에서Dev

How do I save a string of numbers from an ongoing command to a list in a shell script?

분류에서Dev

How do I go about converting temperatures using C program in shell script?

분류에서Dev

How do I write a shell script to issue commands and see the output on the screen

분류에서Dev

How do I echo a TAB char on a command prompt

분류에서Dev

How can I do subdirectory manipulation in shell?

분류에서Dev

How do I set the at command shell to bash?

분류에서Dev

How can I insert values into a MySQL table which contain whitespaces

분류에서Dev

How to cd into a directory as part of shell script and do a word count of files?

분류에서Dev

How do run MYSQL scripts from a shell script with bash variables?

분류에서Dev

How do I preserve newline character in Spring while sending data through a JSP form

분류에서Dev

How do I edit a file and preserve its access control list / SELinux security context?

분류에서Dev

How can I get my external IP address in a shell script?

분류에서Dev

How can i compare strings of two files in shell script?

분류에서Dev

How do I run a pitest ant script

Related 관련 기사

  1. 1

    How do I allow whitespaces in this regex?

  2. 2

    How do I Successfully Echo a For Loop Into a Batch Script?

  3. 3

    How do I replace a whitespace with any number of whitespaces?

  4. 4

    How do I switch desktop session users from a shell script?

  5. 5

    how do i create a shell script with multiple choice menu variables?

  6. 6

    How do I pass a variable from a shell script to my .muttrc?

  7. 7

    How can I do basic calculations in a shell script?

  8. 8

    How do I bring HEREDOC text into a shell script variable?

  9. 9

    How do I run a windows executable in linux shell script?

  10. 10

    How do I prevent a script from terminating when the shell exits?

  11. 11

    How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

  12. 12

    How do I preserve the current viewport when removing a div?

  13. 13

    How to do float operation in Shell script?

  14. 14

    How to do string comparison properly in shell script?

  15. 15

    How do I find the latest date folder in a directory and then construct the command in a shell script?

  16. 16

    How do I save a string of numbers from an ongoing command to a list in a shell script?

  17. 17

    How do I go about converting temperatures using C program in shell script?

  18. 18

    How do I write a shell script to issue commands and see the output on the screen

  19. 19

    How do I echo a TAB char on a command prompt

  20. 20

    How can I do subdirectory manipulation in shell?

  21. 21

    How do I set the at command shell to bash?

  22. 22

    How can I insert values into a MySQL table which contain whitespaces

  23. 23

    How to cd into a directory as part of shell script and do a word count of files?

  24. 24

    How do run MYSQL scripts from a shell script with bash variables?

  25. 25

    How do I preserve newline character in Spring while sending data through a JSP form

  26. 26

    How do I edit a file and preserve its access control list / SELinux security context?

  27. 27

    How can I get my external IP address in a shell script?

  28. 28

    How can i compare strings of two files in shell script?

  29. 29

    How do I run a pitest ant script

뜨겁다태그

보관