Grep util last occurrence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep util last occurrence
# 1  
Old 04-19-2018
Grep util last occurrence

I have file contents

Code:
/tmp/x/abc.txt
/home/bin/backup/sys/a.log

I need this output:
Code:
/tmp/x/
/home/bin/backup/sys/

Can somebody please help me out

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-19-2018 at 05:46 PM.. Reason: Added CODE tags.
# 2  
Old 04-19-2018
Isn't that what the command dirname does? It only does one line at a time, though.

P.S. This is another way:
Code:
sed 's/\/[^\/]*$//'

The final operand (not shown) is the name of your input file.

Last edited by wbport; 04-19-2018 at 05:35 PM.. Reason: Add P.S.
# 3  
Old 04-19-2018
that worked

ty
# 4  
Old 04-19-2018
/ at the end needed?

A couple more ways from the many ways to skin the cat:
Code:
awk '{$NF=_} 1' FS=/ OFS=/ file

Code:
while read line ; do echo "${line%/*}/" ; done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

3. UNIX Desktop Questions & Answers

grep a specific amount of occurrence

hey , i m trying to figure out how to do the following : i got a text file the looks like so: 1031 1031 1031 1031 1031 1031 1031 1031 16500 16500 16500 16500 1031 1031 (4 Replies)
Discussion started by: boaz733
4 Replies

4. HP-UX

glance => network util variable?

Hi, I'm creating a preventive maintenance script (automated) and I would like to get the cpu, mem, disk & network stats which you get when running glance as an output of a command so I can use it in my script I made some progress though: this is the command I use glance -aos... (5 Replies)
Discussion started by: berre
5 Replies

5. UNIX for Dummies Questions & Answers

grep first occurrence but continue to next entry in patternfile

I have 1300 files (SearchFiles0001.txt, SearchFiles0002.txt, etc.) , each with 650,000 lines, tab-delimited data. I have a pattern file, with about 1000 lines with a single word. Each single word is found in the 1300 files once. If I grep -f PatternFile.txt SearchFiles*.txt >OutputFile.txt... (2 Replies)
Discussion started by: newhavendweeb
2 Replies

6. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

7. Red Hat

util-linux upgrade with interruption?

Hi All, I have util-linux upgrade that I need to do on redhat 4 ent. Does this going to interruption with the operation of the OS or application? Or this will be straight forward upgrade that will not make a harm or downtime of the OS. Thank you for any comments you may add. (3 Replies)
Discussion started by: itik
3 Replies

8. Shell Programming and Scripting

execute command multithreaded util without programming

Hello all is there any way in unix to execute command in multithreaded way without doing it in java or cpp can one of the scripts handle multithread execution ? i need to test server requests ( corba ) in multithread Thanks (0 Replies)
Discussion started by: umen
0 Replies

9. UNIX for Dummies Questions & Answers

grep usage - exit after first occurrence

Hi, Is there any way of using grep (this may be done in awk, not sure?) that I can stop grep'n a file once I have found the first occurrence of my search string. Looking through grep man pages -q will exit without printing the lines after the first match, but I need the output. I have... (5 Replies)
Discussion started by: nhatch
5 Replies

10. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question