Remove the \r \n from the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the \r \n from the variable
# 1  
Old 05-23-2013
Remove the \r \n from the variable

I a have a delimited file that has a 2 column which I need to read and pull out both columns from the file.Now the while pulling out the second column and storing in a variable I get a \r\n charachter embedded in the output when an od -c is run.How can I remove the \r\n columns from the file.

example : File name : XYZ.DAT
Code:
Column 1!$#$!Column 2
ABC!$#$!123
DEF!$#$!456

This is the command I run :
Code:
SN_PRD_FILE_CNT=`cat XYZ.DAT | head -2|tail -1 | awk -F '[!][$][#][$][!]' '{print $2}' |od -c

When storing in a variable and redirecting the same to an output file I get the enter (\r\n) included.need to attain only 123
# 2  
Old 05-23-2013
It's a bit unclear. You mean both columns from all rows, or both second columns, from the file?

Show what output you expect.

Code:
$ SN_PRD_FILE_CNT=$($ awk -F '[$#!]' 'NR==2{print $1,$NF}' ORS=" " XYZ.DAT)
$ echo $SN_PRD_FILE_CNT                                                                              
ABC 123 DEF 456


Last edited by Scott; 05-23-2013 at 12:16 PM.. Reason: Third time lucky
# 3  
Old 05-23-2013
Another awk approach:
Code:
awk '{gsub(/.*!|\r/,X);print}' ORS= XYZ.DAT | od -c

To store it in variable:
Code:
SN_PRD_FILE_CNT=$( awk '{gsub(/.*!|\r/,X);print}' ORS= XYZ.DAT )

# 4  
Old 05-23-2013
Code:
SN_PRD_FILE_CNT=`cat XYZ.DAT | head -2|tail -1 | perl -pne 'chomp' - |awk -F '[!][$][#][$][!]' '{print $2}' |od -c


Last edited by Scott; 05-23-2013 at 01:59 PM.. Reason: Code tags?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove newline char from variable

I have a file ABC.DAT with 2 columns avaialble Data format : XYZ!$#$!120 XXZ!$#$!1000 YYZ!$#$!104 While running the following code : FILE_COUNTER=1; RECORD_CN_FILE_COUNT=$((`wc -l ABC.DAT| cut -f1 -d' '`)); while do FILE_NAME=`cat ABC.DAT.DAT| head -$FILE_COUNTER |tail -1 | awk -F... (1 Reply)
Discussion started by: Nikhil Gautam
1 Replies

2. Shell Programming and Scripting

Remove appending 0 from file variable

Dear All, i have filename RYK3201_032001002.pdf and i am using below command to get a file file_name1=$(echo $file_name | cut -d "_" -f2 | cut -d "." -f1 | cut -c -6) and then file_name2=${NewFile_NAME}_$file_name1 now the value of file_name1 will be 032001 i want to file_name1... (5 Replies)
Discussion started by: yadavricky
5 Replies

3. Shell Programming and Scripting

How to remove new line from variable?

I have following codes: ~$ 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 ~$ echo $var_2 | wc -m would give 3; but apparently, var_2 still has the new line... (5 Replies)
Discussion started by: littlewenwen
5 Replies

4. Shell Programming and Scripting

PERL : Remove spaces in a variable

I have a variable I want to remove the spaces in between. The output should be How can this be done Any help will be appreciated. Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

5. Shell Programming and Scripting

Remove % from a variable

I have the following sh file which I originally wrote for HP Unix(which worked great)... but df is different in AIX (HP doesn't have a % in df -k), So I need to remove the % for $x at the moment I'm getting 68% I need it to be 68 so my script complete. Any help would be welcome.. I'm sure this... (7 Replies)
Discussion started by: KevinGod
7 Replies

6. Shell Programming and Scripting

How to remove trailing spaces from a variable?

I am getting a value from a csv file using CUT command, however the command extracting the records with trailing spaces. I am using the result into a sql session to fetch data, because of the trailing spaces the sql session is unable to fetch any data. Please let me know, how to remove this... (2 Replies)
Discussion started by: mady135
2 Replies

7. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

8. UNIX for Dummies Questions & Answers

Remove characters from string variable

I am running a script where one of the variables (the month and year) is input at the command line. What I would like to do is chop off the last few characters of that string to create a new variable, while maintaining the old one. The script is run like this: ./pull_station_v4.csh KYWST... (3 Replies)
Discussion started by: TheSMan5
3 Replies

9. Shell Programming and Scripting

Remove non numeric values from a variable

Hello all, I am working on a basic script but need a little help. Issue: I am running a SQL Query using sqlplus and a shell script. I have the output of the statement stored as variable $A. $A is set to "other text here 45678754 other text here". I need to strip all text except that numeric... (13 Replies)
Discussion started by: ownedthawte
13 Replies

10. Shell Programming and Scripting

Variable has spaces around the string, need to remove them

Hi all, I'm a newbie to the Linux world and I got a couple of shell script questions: (1) How do combine two variables and make it equal to a third variable? For example, I got a variable $A=FirstName, $B=LastName, and I want to combine the variable into one variable so when you echo the final... (4 Replies)
Discussion started by: mikey20
4 Replies
Login or Register to Ask a Question