- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script.
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
тАО07-27-2000 04:23 AM
тАО07-27-2000 04:23 AM
The extracted lines should contain a given string
AND
The line before the first occurence in the input file should also be extracted
AND
All the extracted lines should be deleted from the input file.
The result should be placed ino a output text file.
I think that an "awk" guru could help me best.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 04:34 AM
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 04:35 AM
тАО07-27-2000 04:35 AM
Re: Shell script.
Instead:
print $0
print prev
use:
print prev
print $0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 06:23 AM
тАО07-27-2000 06:23 AM
Re: Shell script.
your answer helped me a lot but the lines containing the search string are printed twice:
awk '{if ($0 ~ "pattern") {print prev;print $0;} prev=$0;}' inputfile > outputfile
I solved the problem with the following additional commands:
head -1 outputfile > outputfile1
line=`wc -l outputfile | awk '{print $1}'`
line=`expr $line - 1`
tail -$line outputfile | sort | uniq > outputfile2
cat outputfile1 outputfile2 > outputfile
I don't know wether it is possible to do it within the awk command, but it works perfectly.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 06:29 AM
тАО07-27-2000 06:29 AM
Re: Shell script.
The extracted lines should be deleted from the input file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 08:36 AM
тАО07-27-2000 08:36 AM
Re: Shell script.
Below the solution.
Take a look at the -v option of awk to assign a parameter to be passed to the command.
Pat
---------------------------------------------
Shell script
---------------------------------------------
#!/sbin/sh
# Initialisation
search_string=$1
inputfile=$2
outputfile=$3
outputfile1=${outputfile}_tmp1
outputfile2=${outputfile}_tmp2
# Main
awk -v search_string=$search_string '{if ($0 ~ search_string) {print prev;print $0;} prev=$0;}' $inputfile > $outputfile
head -1 ${outputfile} > $outputfile1
nb_lines=`wc -l ${outputfile} | awk '{print $1}'`
nb_lines=`expr $nb_lines - 1`
tail -$nb_lines ${outputfile} | sort | uniq > $outputfile2
cat $outputfile1 $outputfile2 > ${outputfile}
sed -e 's/ /?/g' ${outputfile} > ${outputfile}$$
mv ${outputfile}$$ ${outputfile}
sed -e 's/"/@/g' ${outputfile} > ${outputfile}$$
mv ${outputfile}$$ ${outputfile}
sed -e 's/ /?/g' ${inputfile} > ${inputfile}$$
mv ${inputfile}$$ ${inputfile}
sed -e 's/"/@/g' ${inputfile} > ${inputfile}$$
mv ${inputfile}$$ ${inputfile}
ligne=`head -1 ${outputfile}`
grep -v "$ligne" ${inputfile} > ${inputfile}$$
mv ${inputfile}$$ ${inputfile}
grep -v "$search_string" ${inputfile} > ${inputfile}$$
mv ${inputfile}$$ ${inputfile}
sed -e 's/?/ /g' ${outputfile} > ${outputfile}$$
mv ${outputfile}$$ ${outputfile}
sed -e 's/@/"/g' ${outputfile} > ${outputfile}$$
mv ${outputfile}$$ ${outputfile}
sed -e 's/?/ /g' ${inputfile} > ${inputfile}$$
mv ${inputfile}$$ ${inputfile}
sed -e 's/@/"/g' ${inputfile} > ${inputfile}$$
mv ${inputfile}$$ ${inputfile}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 09:37 AM
тАО07-27-2000 09:37 AM
Re: Shell script.
Let's use Andreas' suggestion amended as follows:
awk '{
if ($0 ~ /pattern/)
{
print $0 >> "/tmp/file.1";
print prev >> "/tmp/file.1";
}
else
{
print $0 >> "/tmp/file.2";
prev=$0;
}
}' /tmp/file
I think you will find that /tmp/file.2 is the input file stripped according to your mandate.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 10:10 AM
тАО07-27-2000 10:10 AM
Re: Shell script.
INFILE=xxxx
OUTFILE=yyyyy
awk '{if($0 ~ "AND")
{
print prev
print $0
}
prev=$0
}' $INFILE > $OUTFILE
while read LINE
do
grep -v "$LINE" $INFILE > tmpfile
mv tmpfile $INFILE
done < $OUTFILE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 11:25 AM
тАО07-27-2000 11:25 AM
Re: Shell script.
mfile=file_before_deletions
file1=mfile_after_deletions
file2=deletions_from_mfile
pattern=your_pattern
count=$(grep -c $pattern $mfile)
#if count = 0 no lines to delete
#
if (( $count == 0 )) ;then
cp $mfile $file1
touch $file2
else
#get line number of first occurance
#
line_num=$(grep -n $pattern $mfile | awk -F: '{print $1;exit;}')
#if the first occurance is the first line
#you can't delete the previous line
#so just delete the lines with the pattern
#
if (( $line_num == 1 )) ;then
grep -v $pattern $mfile > $file1
grep $pattern $mfile > $file2
else
#
# get the line number of the line
# before the first occurance
#
(( line_num = $line_num - 1 ))
# use sed to delete line then grep for all rest
#
sed -e "${line_num}d" $mfile | grep -v $pattern > $file1
#for deletions file use sed to print the line
#before the first occurance then grep the rest
#
sed -ne "${line_num}p" $mfile > $file2
grep $pattern $mfile >> $file2
fi
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2000 01:05 PM
тАО07-27-2000 01:05 PM
Re: Shell script.
ex -s +"/$pattern/ | .- d | 1,\$ global /$pattern/d | w > $file_with_deletions | q!" your_file