LINUX - How to remove the final delimiter from a command output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers LINUX - How to remove the final delimiter from a command output
# 1  
Old 05-30-2012
LINUX - How to remove the final delimiter from a command output

Hi All,

I am trying to list the various dates for which the file is available in a directory using the command below, (& subsequently pass the command output to a loop)

Command :

Code:
ls dir|grep 'filename'|cut -d '_' -f1|cut -c1-8|tr '\n' ','

However, it is giving me an extra comma (highlighted) in the end.

Code:
20111230,20111231,20120101,20120109,20120110,20120111,20120112,

Can you pls help me remove this comma from the end.

Thanks much
Freddie

Last edited by Scrutinizer; 05-30-2012 at 05:07 PM.. Reason: code tags
# 2  
Old 05-30-2012
tr can't tell the difference between 'good' newlines and 'bad' newlines. You could just tag another sed on the end, or...

If you showed your input, it'd probably be simple enough to do this all in one awk instead of five externals connected with pipes.
# 3  
Old 05-30-2012
Hi Corona,

Thanks for your quick response.

My files will look like below,

Code:
20111230_CUST_DLY.dat
20111231_CUST_DLY.dat
20120101_CUST_DLY.dat
20120109_CUST_DLY.dat
20120111_CUST_DLY.dat
20120110_CUST_DLY.dat
20120112_CUST_DLY.dat

Thanks again,
Freddie

Last edited by Scrutinizer; 05-30-2012 at 05:08 PM.. Reason: code tags
# 4  
Old 05-30-2012
Code:
$ ls tmp | awk -F"_" '{ Z=Z","$1 } END { print substr(Z,2) }'
20111230,20111231,20120101,20120109,20120110,20120111,20120112

$

# 5  
Old 05-30-2012
Thanks Corona,

Sorry, if I am asking basic questions. Can you pls let me know what Z mean ? Also is $1 the no of days value ? (or) is it the filename ?

Code:
ls tmp | awk -F"_" '{ Z=Z","$1 } END { print substr(Z,2) }'

Thanks again,
Freddie

Last edited by Scrutinizer; 05-30-2012 at 05:08 PM.. Reason: code tags
# 6  
Old 05-30-2012
Z is a variable name. I could have called it anything.

$1 is the first column.

Since columns are split on _, the first column is the date part.
# 7  
Old 05-31-2012
Thanks Corona,

I tried couple of things today morning & it doesnt seem to give the dates as output.
Code:
 
ls /dev_data/tmp| awk -F"_" '{ Z=Z","$1 } END { print substr(Z,2) }'

This one returned the first word of all the files before the First Underscore.

Is there a way I can pass the filename so that it will restrict only to the file which I am interested in ?

Thanks for your inputs.

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 05-31-2012 at 12:36 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicates separated by delimiter

First post, been browsing for 3 days and came out with nothing so far. M3 C2 V5 D5 HH:FF A1-A2,A5-A6,A1-A2,A1-4 B4-B6,B2-B4,B4-B6,B1-B2output should be M3 C2 V5 D5 HH:FF A1-A2,A5-A6,A1-A4 B2-B4,B4-B6,B1-B2On col 6 and 7 there are strings in form of Ax-Ax and Bx-Bx respectively. Each string are... (9 Replies)
Discussion started by: enrikS
9 Replies

2. UNIX for Beginners Questions & Answers

Remove whitespaces between delimiter and characters

Hello All, I have a pipe delimited file and below is a sample data how it looks: CS123 | | || 5897 | QXCYN87876 As stated above, the delimited files contains sometimes only spaces as data fields and sometimes there are extra spaces before/after numeric/character data fields. My requirement... (4 Replies)
Discussion started by: amvip
4 Replies

3. Shell Programming and Scripting

[SOLVED] Want to remove output from a command

Hi, I'm on AIX 5.2. I wrote a script that makes a traceroute to a host. The script works fine but each time it using the traceroute command its generate the 2 output lines. this is the command in my script traceroute -n -m 5 -w 2 $Host | grep 172 | awk '{print $2}' | tail -1 traceroute... (2 Replies)
Discussion started by: ce9888
2 Replies

4. UNIX for Dummies Questions & Answers

Remove Extra Delimiter

Hi , I have file like this.. aaa|bbbb|cccc|dddd|fff|dsaaFFDFD| Adsads|sas|sa|as|asa|saddas|dsasd|sdad| dsas|dss|sss|sss|ddd|dssd|rrr|fddf| www|fff|refd|dads|fsdf|00sd| 5fgdg|dfs00|d55f|sfds55|445fsd|55ds|sdf| so I do no have any fix pattern and I want to remove extra... (11 Replies)
Discussion started by: pankajchaudhari
11 Replies

5. UNIX for Dummies Questions & Answers

Remove text in particular delimiter

I have input file like this 551|552|553|554|555|556|557|558|559|560 I need any one offset need to be blank for eg. 551|552|553||555|556|557|558|559|560 My Shell is csh (1 Reply)
Discussion started by: nsuresh316
1 Replies

6. Shell Programming and Scripting

remove delimiter

hy all, i need case with input like this 1::||10334|11751| 2::10324|17541||| i want output like this 1::||1033411751| 2::1032417541||| how i can do that for get like output thx before your advice (3 Replies)
Discussion started by: zvtral
3 Replies

7. UNIX for Dummies Questions & Answers

set output delimiter as tab in cut command

I can not make it work, it prints \t rather than introduce tabs. cut -d "," -f 4,8 Samples.csv --output-delimiter="\t" | sort > out Since I am running this command within a shell script, I tried manually inserting tab in this command, still does not work. I am using bash shell Suggestions... (8 Replies)
Discussion started by: analyst
8 Replies

8. Shell Programming and Scripting

How to remove delimiter from specific column?

I have 5 column in sample txt file where in i have to create report based upon 1,3 and 5 th column.. I have : in first and third coulmn. But I want to retain the colon of fifth coulmn and remove the colon of first column.. 5th column contains String message (for example,... (7 Replies)
Discussion started by: Shirisha
7 Replies

9. Shell Programming and Scripting

Remove spaces before a delimiter

Hi All, I need to modify a script to remove spaces from a csv file. The csv file is delimited by the '~' character and I need to remove the spaces which appear before this character. i.e Sample input: LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011 Desired... (5 Replies)
Discussion started by: SRyan84
5 Replies

10. Shell Programming and Scripting

Final Output

Hi There, I am having two output files having the following information: Output1: Name1 0 Name2 222 Name3 598 Name4 9800 Output2: Name1 10 Name2 333 Name3 567 Name4 39003 as you can see the two output files have the same Name colom but different records for each name. Now, how... (4 Replies)
Discussion started by: charbel
4 Replies
Login or Register to Ask a Question