Hello,
I got files full path in a text file like that
/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 out put i want is
/main/k/kdelibs/
/main/k/kdelibs-experimental/
/main/k/kdemultimedia/
/main/k/kdenetwork/
/main/k/kdenetwork/
/main/k/kdepim/
without .deb file name, that will be great if somebody can find solution.
Many thank
bash
while read line;do echo ${line%/*}/;done < file
a=$(<"file")
IFS=" "
printf "%s\n" ${@%/*}
unset IFS
hi..
perl -wln -e 'print $1 if /^(.*\/).*\.deb/'
Regards.
WORKING GREAT, MANY THANKS
thegeek
December 9, 2009, 10:02am
6
if you have dirname command, then you can do this.
$ dirname /main/k/kdelibs/kdelibs4c2a_3.5.10.dfsg.1-2ubuntu7_i386.deb
/main/k/kdelibs
Or:
sed
sed 's [^/]*$ ' infile
zsh
print -l ${(j:/\n:)$(<infile)%/*}/
Perl:
perl -ple's|[^/]*$||' infile
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
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
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
awk 'sub(/[^\/]*$/,_)&&!__[$0]++' infile
With some AWK implementations you don't need to escape the forward slash inside a character class.
Easier to read:
awk '{
sub(/[^\/]*$/, "") # get rid of the filename
if (t[$0]++ == 0) print # print if seen for the first time
}' infile
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
/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
/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
awk 'sub(/[^\/]*$/,"*.deb")&&!__[$0]++' infile
awk '{
sub(/[^\/]*$/, "*.deb") # modify the filename
if (t[$0]++ == 0) print # print if seen for the first time
}' infile
Working great, many thanks
frans
December 9, 2009, 1:19pm
13
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).