HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
Showing results for 
Search instead for 
Did you mean: 

awk / cut script help ..

 
SOLVED
Go to solution
rleon
Regular Advisor

awk / cut script help ..

I have the file with the following info:

/abc/def/myfile.txt
/qwe/rty/yui/myfile2
/a/b/c/d/mylog.log

In other words the files may or may not have extensions and will be in a variety of directories.

I would like my output to be like this:
/abc/def/
myfile.txt

/qwe/rty/yui/
myfile2

/a/b/c/d/
mylog.log

--------------------------------

#!/usr/bin/sh

for file in `cat files`
do
echo $file | do something to get the file name
echo $file | do something to get the directory of the file name
done

Thanks !
6 REPLIES
Solution

Re: awk / cut script help ..

for file in $(< files); do
echo $(dirname $file)
echo $(basename $file)
echo
done

I output the directory first, since that's what your required output format wanted.
Instead of dirname(1) and basename(1) you can use shell Parameter Substitution:
echo ${file%/*}
echo ${file##*/}
TTr
Honored Contributor

Re: awk / cut script help ..

There are two standard HP-UX commands that will do *exactly* what you need in your script above. Take a look at the man pages for "basename" and "dirname"
Hein van den Heuvel
Honored Contributor

Re: awk / cut script help ..



Using PERL ...

$ perl -pe "s;(/[^/]+)$;/\n\1\n;" x
/abc/def/
/myfile.txt

/qwe/rty/yui/
/myfile2

/a/b/c/d/
/mylog.log

s;(/ = substitute a slash
[^/]+ = followed by a bunch of non-slashes
) = which are to be remembered as \1
$ = at the end of a line;
; = and replace with
/\n\1\n; = a fresh slash, a newline, the remembered non-slashes and a bonus newline.


Similar, using AWK as per subject line:

$ perl -pe "s;(/[^/]+)$;/\n\1\n;" x
/abc/def/
/myfile.txt

/qwe/rty/yui/
/myfile2

/a/b/c/d/
/mylog.log


Now if there were multiple entries for one directory...
1) would you perhaps want one header, and multiple details?
2) Even if they are not adjacent in the input stream?

Enjoy,
Hein

Hein van den Heuvel
Honored Contributor

Re: awk / cut script help ..

Oops, bad repeat cut & paste. Meant to offer:

$ awk -F/ '{sub ($NF "$","\n" $NF "\n",$0); print}' x
/abc/def/
myfile.txt

/qwe/rty/yui/
myfile2

/a/b/c/d/
mylog.log


Just change the standard whitespace field seperator by a slash.
Look for the last field value ($NF) by anchoring it at the end of line with that $.
This is such that a file "b" in directory a/b/c would not find the first matching string /b/

Hein
Viktor Balogh
Honored Contributor

Re: awk / cut script help ..

Dennis, you don't need the extra echo ;)

>for file in $(< files); do
> echo $(dirname $file)
> echo $(basename $file)
> echo
>done

for file in $(< files); do
dirname $file
basename $file
echo
done

****
Unix operates with beer.
Arturo Galbiati
Esteemed Contributor

Re: awk / cut script help ..

HI,
small improvement to show the directory only when chnaged (or first time)

DirNam=
for file in $(< files); do
if [[ $DirNam != $(dirname $file) ]]; then
DirNam=$(dirname $file)
echo $DirNam
fi
basename $file
echo
done

HTH,
Art