Remove filename is text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove filename is text file
# 8  
Old 12-09-2009
Many thanks

---------- Post updated at 10:34 AM ---------- Previous update was at 10:16 AM ----------

Many thanks for reply , now i got a little problem getting many same lines like that

PHP Code:
main/a/antlr/
main/a/apache2/
main/a/app-install-data-ubuntu/
main/a/apparmor/
main/a/apport/
main/a/apport/
main/a/apport/
main/a/apport/
main/a/apr-util/
main/a/apr-util/
main/a/apt/
main/a/apt/
main/a/apturl




and the output i want is

PHP Code:
main/a/antlr/
main/a/apache2/
main/a/app-install-data-ubuntu/
main/a/apparmor/
main/a/apport/
main/a/apr-util/
main/a/apt/
main/a/apturl
want to remove lines if there is more than one times.


Many thanks
# 9  
Old 12-09-2009
Code:
awk 'sub(/[^\/]*$/,_)&&!__[$0]++' infile

With some AWK implementations you don't need to escape the forward slash inside a character class.

Easier to read:

Code:
awk '{
  sub(/[^\/]*$/, "")        # get rid of the filename
  if (t[$0]++ == 0) print   # print if seen for the first time  
  }' infile


Last edited by radoulov; 12-09-2009 at 11:58 AM.. Reason: Added readable version.
# 10  
Old 12-09-2009
Many thanks for reply, they are working great , a bit more study , is there any way to replace all .deb files in the text file with *.deb


input file


PHP Code:
/main/k/kdelibs/kdelibs4c2a_3.5.10.dfsg.1-2ubuntu7_i386.deb
/main/k/kdelibs-experimental/libknotificationitem-dev_4.3.2-0ubuntu1_i386.deb
/main/k/kdemultimedia/dragonplayer_4.3.2-0ubuntu1_i386.deb
/main/k/kdenetwork/kget_4.3.2-0ubuntu4_i386.deb
/main/k/kdenetwork/libkopete-dev_4.3.2-0ubuntu4_i386.deb
/main/k/kdepim/kdepim-strigi-plugins_4.3.2-0ubuntu6_i386.deb 
the output file will be like that

PHP Code:
/main/k/kdelibs/*.deb
/main/k/kdelibs-experimental/*.deb
/main/k/kdemultimedia/*.deb
/main/k/kdenetwork/*.deb
/main/k/kdenetwork/*.deb
/main/k/kdepim/*.deb 
Many thanks
# 11  
Old 12-09-2009
Code:
awk 'sub(/[^\/]*$/,"*.deb")&&!__[$0]++' infile


Code:
awk '{
  sub(/[^\/]*$/, "*.deb")   # modify the filename
  if (t[$0]++ == 0) print   # print if seen for the first time  
  }' infile

# 12  
Old 12-09-2009
Working great, many thanks
# 13  
Old 12-09-2009
I'v seen in another post that you wanted to remove duplicate lines
pipe the output or file file through the 'uniq' command and first through a 'sort' command if it's not already sorted.
# 14  
Old 12-09-2009
Quote:
Originally Posted by frans
I'v seen in another post that you wanted to remove duplicate lines
pipe the output or file file through the 'uniq' command and first through a 'sort' command if it's not already sorted.
... or use directly sort -u (if loosing the original order is not an issue).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

How to remove filename from output file?

Hello, I am trying to print searched multiple keywords in multiple files. It is almost okay with the code but the code puts filename in front of each line. How may I get rid of it? -grep -A1 'word1' *.txt | grep -A1 'word2' | grep -A1 'word3' I expect: Real outcome: How may I... (3 Replies)
Discussion started by: baris35
3 Replies

3. Shell Programming and Scripting

How to remove the text between all curly brackets from text file?

Hello experts, I have a text file with lot of curly brackets (both opening { & closing } ). I need to delete them alongwith the text between opening & closing brackets' pair. For ex: Input:- 59. Rh1 Qe4 {(Qf5-e4 Qd8-g8+ Kg6-f5 Qg8-h7+ Kf5-e5 Qh7-e7+ Ke5-f5 Qe7-d7+ Qe4-e6 Qd7-h7+ Qe6-g6... (6 Replies)
Discussion started by: prvnrk
6 Replies

4. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

5. Shell Programming and Scripting

Cat files listed in text file and redirect to new directory with same filename

I have a directory that is restricted and I cannot just copy the files need, but I can cat them and redirect them to a new directory. The files all have the date listed in them. If I perform a long listing and grep for the date (150620) I can redirect that output to a text file. Now I need to... (5 Replies)
Discussion started by: trigger467
5 Replies

6. Shell Programming and Scripting

Remove the last 15 characters of a filename with respect to leave file extension

how can i remove numbers of characters from the last name of file with respect to not remove the files extension example VFX_Official_Trailer_(HD)__Shhh__-_by_Freddy_Chavez_Olmos_&_Shervin_Shoghian-.mp4 i want to rename this to VFX-Official-Trailer-(HD)-Shhh... (13 Replies)
Discussion started by: ateya
13 Replies

7. Shell Programming and Scripting

Cut text from a file and remove

Hello Friends, I am stuck with the below problem.Any help will be appreciated. I have a file which has say 100 lines. On the second last line I have a line from which i want to remove certain characters.. e.g CAST(CAST( A as varchar(50)) || ',' || CAST(CAST( B as varchar(50)) || ',' ||... (8 Replies)
Discussion started by: vital_parsley
8 Replies

8. Shell Programming and Scripting

grabbing filename from text file....should be easy!

Quick question...I'm trying to grab the .tif file name from this output from our fax server. What is the best way i can do this in a bash script? I have been looking at regular expressions with bash or using awk but having some trouble. thanks! The only output i want is... (5 Replies)
Discussion started by: kuliksco
5 Replies

9. Shell Programming and Scripting

remove chunks of text from file

All, So, I have an ldif file that contains about 6500 users worth of data. Some users have a block of text I'd like to remove, while some don't. Example (block of text in question is the block starting with "authAuthority: ;Kerberosv5"): User with text block: # username, users,... (7 Replies)
Discussion started by: staze
7 Replies

10. Shell Programming and Scripting

remove specified text from file

I am trying to write a script that kills old sessions, I've posted here over the past few days and the script is just about perfect except I want to be given the option to exclude specified PIDs from being killed. this is the entire script: if then rm /tmp/idlepids fi if then rm... (2 Replies)
Discussion started by: raidzero
2 Replies
Login or Register to Ask a Question