- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- how to paser this string
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
Forums
Discussions
Discussions
Discussions
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
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
05-15-2007 11:43 AM
05-15-2007 11:43 AM
I have not script for a while....and need your help again..
say if I have a line like this..
list="Only in /usr/local/bin Only in /usr/lib/bin Only in /home/smith"
I parse that one string into 3 strings such as
Only in /usr/local/bin
Only in /usr/lib/bin
Only in /home/smith
I used to know how to do this...
please help out!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2007 01:13 PM
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2007 01:53 PM
05-15-2007 01:53 PM
Re: how to paser this string
...and if you want a (longer) 'awk' solution:
# echo ${list} | awk '{while (match($0,/Only in [A-Za-z\/]+/)) {S=substr($0,RSTART,RLENGTH);print S;$0=substr($0,RLENGTH)}}'
Regards!
...JRF...
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2007 03:04 PM
05-15-2007 03:04 PM
Re: how to paser this string
Speficifcally... Will a new chunk always start with space+"Only in", or just "Only", or...
$ echo $list | perl -pe 's% (Only in)%\n$1%g'
Only in /usr/local/bin
Only in /usr/lib/bin
Only in /home/smith
If it always starts with whatever the first chunk starts with, and there is a space, then you could use:
$ echo $list | perl -pe '$x=substr($_,0,5); s/\s$x/\n$x/g'
Only in /usr/local/bin
Only in /usr/lib/bin
Only in /home/smith
$
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 05:48 AM
05-16-2007 05:48 AM
Re: how to paser this string
I would like to try the perl command, but, I am still not getting this one quite right, please help me.
the input is
Only in /usr/local/bin: a.config Only in /usr/lib/bin: b.conf Only in /home/smith: c.conf
Notice there is no "space" before "Only in", but now, I add the file name...how do I include the file name....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 05:56 AM
05-16-2007 05:56 AM
Re: how to paser this string
OK, given:
# list="Only in /usr/local/bin: a.config Only in /usr/lib/bin: b.conf Only in /home/smit
h: c.conf"
...then:
# echo ${list} | perl -nle 'print $1 while /(Only in\s+\S+\s\S+)/g'
...yields:
Only in /usr/local/bin: a.config
Only in /usr/lib/bin: b.conf
Only in /home/smith: c.conf
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 06:36 AM
05-16-2007 06:36 AM
Re: how to paser this string
...and for similar changes to the 'awk':
# echo ${list} | awk '{while (match($0,/Only in ([A-Za-z\/\:]+ [A-Za-z\.]+ *)/)) {S=substr($0,RSTART,RLENGTH);print S;$0=substr($0,RLENGTH)}}'
Note, of course, that with the 'awk' snippet, filenames containing numbers or special characters would fail the match. I'll leave that as an exercise for you!
The Perl solutions using '\s' for a whitespace and '\S' for a nonwhitespace is easier (as Perl is!). For that matter, Hein's solutions are quite elegant too, and required no changes.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 07:36 AM
05-16-2007 07:36 AM
Re: how to paser this string
# echo $list | awk -F"Only" '{for(i=1;i<=NF;++i) if($i) print FS$i}'
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 07:45 AM
05-16-2007 07:45 AM
Re: how to paser this string
That's a VERY elegant 'awk' solution! Shorter AND sensitive only to matching the keyword "Only". I always learn better ways with 'awk' and 'sed' reading your contributions.
/* NO POINTS for this post, please */
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 07:57 AM
05-16-2007 07:57 AM
Re: how to paser this string
:D
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 08:43 AM
05-16-2007 08:43 AM
Re: how to paser this string
I want to diff two directories, and only display the "Only In" result.
Is there an option in the diff that can do that? so, I dont even have to parse the result.
Mimosa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 09:07 AM
05-16-2007 09:07 AM
Re: how to paser this string
> I want to diff two directories, and only display the "Only In" result
See the manpages for 'dircmp' :
http://docs.hp.com/en/B2355-60105/dircmp.1.html
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 09:07 AM
05-16-2007 09:07 AM
Re: how to paser this string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 09:19 AM
05-16-2007 09:19 AM
Re: how to paser this string
Something like this:
---------------------------------------
#!/usr/bin/sh
typeset TDIR=${TMPDIR:-/var/tmp}
typeset PROG=${0##*/}
typeset T1=${TDIR}/X${$}_1.tmp
typeset T2=${TDIR}/X${$}_2.tmp
trap 'eval rm -f ${T1} ${T2}' 0 1 2 15
typeset -i STAT=0
if [[ ${#} -ge 2 ]]
then
typeset D1=${1}
typeset D2=${2}
shift 2
if [[ -d "${D1}" && -d "${D2}" ]]
then
ls D1 | sort > ${T1}
ls D2 | sort > ${T2}
comm -3 ${T1} ${T2}
STAT=${?}
else
echo "${PROG}: ${D1} and/or ${D2} not found and/or not directories." >&2
STAT=254
fi
else
echo "Usage: ${PROG} dir1 dir2" >&2
STAT=255
fi
exit ${STAT}
-----------------------------------------
If I didn't make no typing booboo's; that should work. Use it like "dirdiff.sh dir1 dir2". Files that are unique to dir1 appear in column1 of stdout and files unique to dir2 appear in column2.
Man comm for details. You could easily separate this into 3 output files but that is left for you.
- Tags:
- comm
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 09:23 AM
05-16-2007 09:23 AM
Re: how to paser this string
----------------------------------------
#!/usr/bin/sh
typeset TDIR=${TMPDIR:-/var/tmp}
typeset PROG=${0##*/}
typeset T1=${TDIR}/X${$}_1.tmp
typeset T2=${TDIR}/X${$}_2.tmp
trap 'eval rm -f ${T1} ${T2}' 0 1 2 15
typeset -i STAT=0
if [[ ${#} -ge 2 ]]
then
typeset D1=${1}
typeset D2=${2}
shift 2
if [[ -d "${D1}" && -d "${D2}" ]]
then
ls ${D1} | sort > ${T1}
ls ${D2} | sort > ${T2}
comm -3 ${T1} ${T2}
STAT=${?}
else
echo "${PROG}: ${D1} and/or ${D2} not found and/or not directories." >&2
STAT=254
fi
else
echo "Usage: ${PROG} dir1 dir2" >&2
STAT=255
fi
exit ${STAT}
-----------------------------------------
Dircmp may be too intensive for you because it not only does file names but also compares the files themselves.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 10:04 AM
05-16-2007 10:04 AM
Re: how to paser this string
I want to learn different ways to parse the strings anyway. I read every post and try to understand it.
you guys are being extremely helpful.
really appreciate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 10:32 AM
05-16-2007 10:32 AM
Re: how to paser this string
# VAR=$(diff /dir1/dir2 /dir1/dir3)
then echo'ing the doubly-quoted VAR will do the trick w/o any scripting...
# echo "$VAR"
~hope it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2007 08:32 PM
05-16-2007 08:32 PM
Re: how to paser this string
why not the old good Unix command diff?
/> ls -1 t1
a
b
/>ls -1 t2
b
c
/> diff t1 t2
Only in t1: a
Only in t2: c
It shows only the file that are not in both dirs.
HTH,
Art