How to remove new line from variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove new line from variable?
# 1  
Old 04-20-2013
How to remove new line from variable?

I have following codes:

Code:
~$ var_1="ABC"
~$ echo $var_1 | wc -m
4
~$ echo -n $var_1 | wc -m
3
~$ var_2=`echo -n $var_1`
~$ echo $var_2
ABC
~$ echo $var_2 | wc -m
4
~$

I would suppose the last command
Code:
~$ echo $var_2 | wc -m

would give 3; but apparently, var_2 still has the new line character and has 4 character in total.


So how to remove this new line character from a variable? Thanks.
# 2  
Old 04-20-2013
The variable does not have a newline.

The echo command adds the newline, which "wc -m" counts. That's why you get a different count with "echo -n", which does not add a newline.
This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-20-2013
The echo command writes its argument to standard output, followed by a <newline>. If there are no arguments, only the <newline> is written.
Code:
$ echo | od -c
0000000  \n
0000001

This is why you have to use -n option to suppress the trailing <newline>
Code:
$ echo -n | od -c
0000000

You can also use built-in printf instead:
Code:
$ printf "%s" "$var_1" | wc -m
3

This User Gave Thanks to Yoda For This Post:
# 4  
Old 04-20-2013
Thank you very much for educating me.

Now suppose I have following codes:

Code:
$ var999=abc$'\n'def$'\n'defabc$'\n'def$'\n'def
$ echo "$var999"
abc
def
defabc
def
def
$

var999 has several new line characters; is there a quick way to find out: how many new line characters (and how may other characters) are in this variable?
# 5  
Old 04-20-2013
That's a little tricky. I would suggest the following as the best simple solution:
Code:
$ var999=abc$'\n'def$'\n'defabc$'\n'def$'\n'def
$ echo -n "$var999" | wc -l
4

Reports the right number of newline characters.

------------------------
Code:
$ var999=abc
$ echo -n "$var999" | wc -l
0

Verifies that works for case where no newlines.

------------------------
Code:
$ var999=abc$'\n'def$'\n'defabc$'\n'def$'\n'def
$ echo -n "$var999" | wc -m
23

Shows the total number of characters in the variable.

-------------------

This can get kind of complicated and confusing. Consider the following, where with the -e option '\n' is translated to newline character:
Code:
$ var999="abc\nabc"
$ echo $var999
abc\nabc
$ echo -e $var999
abc
abc
$ echo $var999 | wc -m
9
$ echo -e $var999 | wc -m
8

This User Gave Thanks to hanson44 For This Post:
# 6  
Old 04-20-2013
Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

3. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

4. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

5. UNIX for Dummies Questions & Answers

Remove multi line and single line comments

Hi, I am trying to remove multi line and single line comments like examples below I have tried this pattern. it works fine for single line comments and multi line comments in a single line only. but this fails when the comments are extended in multiple lines as shown in the comment 2 of... (3 Replies)
Discussion started by: ahmedwaseem2000
3 Replies

6. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

7. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies

8. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

9. Shell Programming and Scripting

Remove header(first line) and trailer(last line) in ANY given file

Hi, I need some help in removing the header (first line) and the trailer (last line) in a give file... The data file actually comes in EBCDIC format and I converted it into ASCII.. Now I need to strip off the first line and the last line.. I think we can use sed to do something like this:... (2 Replies)
Discussion started by: madhunk
2 Replies
Login or Register to Ask a Question