- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: awk / cut script help ..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2009 06:48 PM
тАО12-06-2009 06:48 PM
/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 !
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2009 07:29 PM
тАО12-06-2009 07:29 PM
Solutionecho $(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##*/}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2009 07:33 PM
тАО12-06-2009 07:33 PM
Re: awk / cut script help ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2009 08:53 PM
тАО12-06-2009 08:53 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2009 08:56 PM
тАО12-06-2009 08:56 PM
Re: awk / cut script help ..
$ 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2009 06:28 AM
тАО12-08-2009 06:28 AM
Re: awk / cut script help ..
>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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-09-2009 02:47 AM
тАО12-09-2009 02:47 AM
Re: awk / cut script help ..
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