The while-read loops are unnecessary. They may also be buggy, since the goal is to convert whitespace and with the default value of IFS, the shell will discard leading and trailing whitespace before assigning a value to $line.
The echo statements are also unnecessary and are also problematic, because, if the line that's read looks like a command line option, it may alter the value of $line or even generate a syntax error. When it's necessary (here it's not), the best way to echo arbitrary text is to use printf '%s\n' "$line".
I don't see the point of the trap. - means stdin; stdin may not be a terminal. If the trap is meant as a way to give a human user a way to abort and continue to the next file in the for-loop list, it's redundant; ^D (control-d) will do the job by sending EOF (end-of-file).
The same goes for the quit|QUIT check in the case-statement. stdin may not be a terminal, even if there is no file argument.
Finally, forking a shell and exec'ing an instance of tr for every line read is very inefficient.
The simplest, most efficient solution is to redirect stdin using exec before invoking tr.
Hi Alister,
Thanks for your reply, let me realize that the while - read loop is totally unnecessary, I made the problem more complicated, since it is so simple. and you are very right that without set the IFS, the read line would trim the leading spaces.
I think this is a simple solution, thanks again, thanks.
This User Gave Thanks to Lucas_0418 For This Post:
Hi Alister,
Thanks for your reply, let me realize that the while - read loop is totally unnecessary, I made the problem more complicated, since it is so simple. and you are very right that without set the IFS, the read line would trim the leading spaces.
I think this is a simple solution, thanks again, thanks.
This worked perfectly, thank you.
---------- Post updated 03-25-14 at 11:16 AM ---------- Previous update was 03-24-14 at 10:03 PM ----------
Quote:
Originally Posted by Lucas_0418
Hi Alister,
Thanks for your reply, let me realize that the while - read loop is totally unnecessary, I made the problem more complicated, since it is so simple. and you are very right that without set the IFS, the read line would trim the leading spaces.
I think this is a simple solution, thanks again, thanks.
Hello, sorry, but actually, what is this line of code for?
because if I try
./function file.txt
it returns this line first
and then it is followed with the correct output. Why is that? How do I get rid of the first line?
Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position?
I am asking this because the file I have doesn't always follow a pattern.
For example the file I have is the result of a command to obtain windows ACLs:
icacls C:\ /t... (5 Replies)
Hello everyone!
I'm trying to make the below file1 look like file2, can anyone help?
Basically I just hit backspace on every line that starts with a number.
Thanks!
file1:
THIS#IS-IT1
4
THIS#IS-IT2
3
THIS#IS-IT3
2
THIS#IS-IT4
1
Result > file2: (4 Replies)
Hi All.
How can I convert this:
ABC_1_1
ABC_1_2
ABC_1_3
into this:
ABC_1 1
ABC_1 2
ABC_1 3
I tried this command but it is not working:
awk '{sub(/+$/,"\t", $1)}{print}'
Any suggestions on how to fix this?
Thank you :wall:
Please use code tags when posting data and... (3 Replies)
I cannot seem to get this to work..
I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application.
the file looks like this
this is a test
that is a test
balblblablblhblbha... (1 Reply)
...when the lines use both a colon and commas to separate the parts you want read as information.
The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Hi
Following is an example line.
echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:"
I want it's output to be
200
However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Greetings
I need to replace "whitespace" in a file with the newline character aka carriage return
My command is either wrong or not interpreted properly by me shell
sed s/" "/\\n" "/g nets > nets1
or
sed s/" "/\n" "/g nets > nets1
nets (input file)
13MHZ_IN... (4 Replies)
All,
I'm a newbie at shell scripting and regular expressions and I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but... (7 Replies)
Hello all
i have big test file that has allot of structure text something like this :
<foo1 *.html>
<blah action>
somthing 1
somthing 2
</blah>
</foo1 >
now i will like to insert 2 more lines of text below the <blah action>
so it will be like :
<foo1... (1 Reply)