awk - help in removing some text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - help in removing some text
# 1  
Old 06-02-2017
awk - help in removing some text

Hey Guys,

Earlier I asked a question under Solaris, which I got great help... thanks.

Although I got the script working for what it really needed to do, I am looking for a bit of help to change the output for nicer reading.

my script gets a list of zones under a global-zone and puts this to a text file e.g
/newpool/server1
/testpool/server2
Code:
###  get the zone admin list
zoneadm list -iv | awk '{ print $4 }' | awk ' length>3 ' | grep -v "PATH" | sort >> $ZONES

in a while loop I output the zones and then show, under root, what reboots have been set up (to see if any have been hashed out)

in my loop I echo the zone appending another text file
Code:
while read ZONE
do
    echo "$ZONE"  >> $MSG
    echo "--------------------------------------" >> $MSG
    cat $ZONE/root/var/spool/cron/crontabs/root | grep "/shutdown" >> $MSG
    echo "\n" >> $MSG
done < "$ZONES"

I want to change the output of echo "$ZONE >> $MSG to remove "/pooltype/" and only echo out the server name.

I have tried adding grep - "/*/" or "\/\*\/" but this works but either does not show any server ("/*/") or shows the server with pool ("\/\*\/")

But I am thinking AWK might be better for what I am trying to do but I can't find anything with awk on how to do this, can anyone suggest how to do this?

I have never used sed so don't know anything about that.

thanks in advance

davy
# 2  
Old 06-02-2017
You are doing this on Solaris? Are you using /bin/sh, /bin/ksh or /bin/bash?
I assume $ZONE contains, eg, /newpool/server1 and you want it to appear as server1.
In /bin/sh, as it looks like a filepath, use
Code:
echo `basename $ZONE` >> $MSG

or even
Code:
basename $ZONE >> $MSG

For ksh and bash, try
Code:
echo ${ZONE##*/} >> $MSG

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 06-02-2017
Quote:
Originally Posted by apmcd47
You are doing this on Solaris? Are you using /bin/sh, /bin/ksh or /bin/bash?
I assume $ZONE contains, eg, /newpool/server1 and you want it to appear as server1.
In /bin/sh, as it looks like a filepath, use
Code:
echo `basename $ZONE` >> $MSG

or even
Code:
basename $ZONE >> $MSG

For ksh and bash, try
Code:
echo ${ZONE##*/} >> $MSG

Andrew
Thanks apmcd47 your absolutely correct, it is in Solaris, and i have the script as /bin/sh

I tried your first option of
Code:
echo `basename $ZONE` >> $MSG

which worked

coming from windows support not long started my first unix admin job, so learning lots quickly...

thanks a million..

davy
# 4  
Old 06-02-2017
How about doing all of this in one single awk script only? The following is for showing the concept only; not having access to a solaris system, I can't test my proposal.

Quote:
Originally Posted by dakelly
.
.
.
Code:
###  get the zone admin list
zoneadm list -iv | awk '{ print $4 }' | awk ' length>3 ' | grep -v "PATH" | sort >> $ZONES

means: get field 4 from the zoneadm list if its longer than three chars and does not contain the "PATH" string. Doesn't
Code:
zoneadm list -iv | awk '(length($4) > 3) &&( $4 !~ "PATH") {print $4)'

do the same?
Quote:
.
.
.
Code:
while read ZONE
do
    echo "$ZONE"  >> $MSG
    echo "--------------------------------------" >> $MSG
    cat $ZONE/root/var/spool/cron/crontabs/root | grep "/shutdown" >> $MSG
    echo "\n" >> $MSG
done < "$ZONES"

.
.
.
means: for all zones found above print the lines containing "/shutdown" from the crontab files for all the zones found.
Doesn't
Code:
zoneadm list -iv | 
awk '
(length($4) > 3) && 
($4 !~ "PATH")  {CRP = $4 "/root/var/spool/cron/crontabs/root"
                 while (1 == getline LN < CRP)  if (LN ~ /\/shutdown/)  {print $4
                                                                         print "---------"
                                                                         print LN
                                                                        }
                 close (CRP)
                }
' > $MSG

do the same?
# 5  
Old 06-02-2017
On a Solaris/SunOS system, you'll probably need to change:
Code:
zoneadm list -iv | awk '(length($4) > 3) && ($4 !~ "PATH") {print $4)'

to:
Code:
zoneadm list -iv | nawk '(length($4) > 3) && ($4 !~ "PATH") {print $4)'

or:
Code:
zoneadm list -iv | /usr/xpg4/bin/awk '(length($4) > 3) && ($4 !~ "PATH") {print $4)'

You will also need to use the same change from awk to nawk or /usr/xpg4/bin/awk in the longer script RudiC suggested.

Last edited by Don Cragun; 06-02-2017 at 09:57 PM.. Reason: Add note about 2nd awk script.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing \n from a text file

Hi All, I have a question regarding text substitution. I have a file that contains a lot of text. Some of the text is separated with a \n like: TEST\nTEST2\nTEST3 BLA\nBLA2\nBLA3 So there are both actual newlines and 'used to be newlines' in the text. using tr tr "\n" "," or... (5 Replies)
Discussion started by: JaapSchuurman
5 Replies

2. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

3. Shell Programming and Scripting

Removing " from a text using awk

I was testing some and from this string try to remove all ", but not \" cat file The quick brown fox "jumps", over the 'lazy \"dog\"' result requested: The quick brown fox jumps, over the 'lazy \"dog\"' I have seen a working solution for sed, but I like awk :) This code seem to work,... (19 Replies)
Discussion started by: Jotne
19 Replies

4. Shell Programming and Scripting

removing lines without text

How do I remove line that do not contain text, but that do contain tabs? I have tried the command cat file | awk NF but that doesn't work when the lines contain tabs (and spaces). I have also tried: cat file | sed '/^$/d' (9 Replies)
Discussion started by: locoroco
9 Replies

5. Shell Programming and Scripting

Removing zero values from text file

Hi all, I wrote the following code to remove the value which are 0 in the input file (a columns if numbers). awk 'BEGIN { for (i=1; i<=NF; i++) if ($i) printf("%13.6e\n",$i) }' $1 >> $2 The script works if the zeros are written as 0.0000 but not as 0.000000e+00 In... (10 Replies)
Discussion started by: f_o_555
10 Replies

6. Shell Programming and Scripting

Help with removing line from text

Hey everyone, I kinda new to shell programming and learning bits and pieces of stuff from tutorials. I got this problem, where I asked my user to enter a string, which will delete a specific line in the string, which I'm unable to do it. Text file(BookDB.txt) as shown: Three Little... (4 Replies)
Discussion started by: aLHaNz
4 Replies

7. Shell Programming and Scripting

Removing text between two static strings

Hi everyone, I need to replace the text between two strings (html tags) and I'm having trouble figuring out how to do so. I can display the text with sed but I'm not having any luck deleting the text between the two strings. My file looks like this: <oths>test</oths><div class="text">1928... (2 Replies)
Discussion started by: cg2
2 Replies

8. Shell Programming and Scripting

Removing matching text

Hello Everyone! Of course I am rather new to these forums, but I have been browsing the forums for awhile, always has a lot of useful information :) This time however, I can not find the information I need, well, not completely. So here is the problem, I am a linux admin (dealing mostly with... (1 Reply)
Discussion started by: Rhije
1 Replies

9. Shell Programming and Scripting

Removing lines in a text file.

Here is my problem I'm hoping you guru's can help me figure out. I have a text file that contains comma delimited columns. What I'm looking to do is see if the 24th column on each row in the file contains a value (not null), and then write/append that line to a different file. I've been... (4 Replies)
Discussion started by: WABonnett
4 Replies

10. UNIX for Dummies Questions & Answers

removing commas from text file

Dear all I have a file which looks like this xxxxxxxxxxxxxx,xxx,xxxxxxxxxx xxxxxxxxxxxxxx,xxx,xxxxxxxxxx etc basically 14 characters then a comma, three characters, then a comma then 10 characters. We are uploading this file to our mainframe and they want the commas removed, so it... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question