Return all characters to the left of the last delimeter of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return all characters to the left of the last delimeter of each line
# 1  
Old 06-28-2012
Return all characters to the left of the last delimeter of each line

Hello,

Working on a ksh script and a little stumped... how can I return all characters to the left of the last delimeter per line in a file, skipping any lines without that delimeter?

ie, sample.txt:

Code:
Once_upon-a-Midnight_dreary_while
I pondered, weak_and weary
over many a quaint
and curious volume_of_forgotten lore,

while I nodded, nearly_napping, suddenly_
there came _ a tapping
_as of someone gently rapping, rapping_at my
_chamber door.

Tis some visitor! I muttered_rapping at my chamber door.
Only this, and nothing more.

If delimeter is "_", output should be:

Code:
Once_upon-a-Midnight_dreary
I pondered, weak
and curious volume_of
while I nodded, nearly_napping, suddenly
there came 
_as of someone gently rapping, rapping

Tis some visitor! I muttered

Thanks for any pointers.
# 2  
Old 06-28-2012
Code:
sed -n 's/_[^_]*$//p' infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 06-28-2012
Thank you - would you also use sed if performing the same string manipulation on a variable in a script?
# 4  
Old 06-28-2012
I wouldn't. It's more efficient to use parameter expansion itself.
Code:
${var%_*}

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 06-28-2012
neat. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

The English characters distorted after add a right to left language

i need for a right to left language support, in red hat EL6 , for repository problem, i never could to use from yum-solution, when i try from Gnu Desktop: Desktop --> system --> preference --> keyboard --> layouts --> Add and Add second language,the second language is ok but i lose English... (4 Replies)
Discussion started by: alwaystudent
4 Replies

2. Shell Programming and Scripting

Split line of file from delimeter.

I have a below file. INPUT FILE select * from customer MERGE INTO Archive; delete from Employee; using select * from customer; delete from employee; select * from Employee; insert into employee(1,1); OUTPUT FILE select * from customer MERGE INTO Archive delete from Employee using... (5 Replies)
Discussion started by: Mohin Jain
5 Replies

3. Shell Programming and Scripting

Replacing the delimeter with other delimeter

Hi Friends, I have a file1.txt as below 29123973Ç2012-0529Ç35310124Ç000000000004762Ç00010Ç20Ç390ÇÇÇÇF 29123974Ç20120529Ç35310125Ç0000000000046770Ç00010Ç20Ç390ÇÇÇÇF 29123975Ç20120529Ç35310126Ç0000000000046804Ç00010Ç20Ç390ÇÇÇÇF 29123976Ç20120529Ç35310127Ç0000000000044820Ç00010Ç20Ç390ÇÇÇÇF i have a file2.txt... (4 Replies)
Discussion started by: i150371485
4 Replies

4. Shell Programming and Scripting

line carriage return characters

Hi, I would like to insert the line carriage retrun characters on each line. (2 Replies)
Discussion started by: koti_rama
2 Replies

5. Web Development

split line based on delimeter SQL

I have a SQL query SELECT BLAH_ID, BLAH_CODE, DATEFORMAT(CALENDAR_DATE, 'MM-DD-YYYY') AS CALENDAR_DATE_STR, HOURS, 'N', FROM blah_tmp_tbl order by CALENDAR_DATE_STR,BLAH_ID,BLAH_CODE; OUTPUT TO 'MyExport.CSV' quote '' FORMAT ASCII; That gets me the below output; ... (2 Replies)
Discussion started by: ffdstanley
2 Replies

6. Shell Programming and Scripting

Script to return first x characters from file

I need a script to read the first 1000 characters of a text file regardless of how many lines it will span. So if my first line has 2000 characters i only want the first 1000 from this line, if my first and second lines have 500 character each then i want all the characters from both lines and... (5 Replies)
Discussion started by: kelseyh
5 Replies

7. Shell Programming and Scripting

append delimeter count for each line in text file

Hi guys, plz tell me how to achieve this how to delete the lines in a file using sed command (6 Replies)
Discussion started by: hari908
6 Replies

8. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

9. Shell Programming and Scripting

How to cut a line with the delimeter "pipe symbol"

Hi I am unable to cut a line with the delimeter as a pipe symbol "|" COuld you please help me ? below is the code i am using right now ************ for i in `cat xyz` do source=`echo $i | cut -f 1 -d |` echo $source done ********* Error i am recieving while exceuting the above... (7 Replies)
Discussion started by: Tasha_T
7 Replies

10. UNIX for Dummies Questions & Answers

Removing carriage return characters from file

Hello there, I need to remove carriage return characters (\n and \r) from any input file specified. This is what I am doing right now: - dumping the file to octal format using the command 'od -c file_name - removing and \s and \n characters using sed commands What I need to do now is... (3 Replies)
Discussion started by: b1saini
3 Replies
Login or Register to Ask a Question