- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: awk/sed/tr problem
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
03-07-2005 03:58 AM
03-07-2005 03:58 AM
I have a file of the format:
start: 1001
12
32
34
35
36
start: 1002
asd
we
23
I want to format this output to read:
start: 1001, 12, 32, 34, 35, 36
start: 1002, asd, we, 23
..so in other words remove line feeds and use the keywords start,and a blank line to delimit the start and end of a record (as you can see these records are of variable length. So I know I can remove line feeds with things like tr (tr -d '\n' < file.txt), but can't work out how I can get this to work with record delimiters. I tried stuff in awk and sed but can't work this one out - any ideas?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:27 AM
03-07-2005 04:27 AM
Re: awk/sed/tr problem
#!/usr/local/bin/perl
foreach $line(`cat input`)
{
$line =~ s/\s+$//;
if ($line eq "") { next; }
if(grep/start/,$line)
{
print "\n";
print "$line, ";
}
else { print "$line, "; }
}
print "\n";
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:27 AM
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:30 AM
03-07-2005 04:30 AM
Re: awk/sed/tr problem
newline=""
infile=./testfile
outfile=./test.out
> $outfile
cat $infile | while read line
do
if [[ -z $line ]] ; then
newline="TRUE"
elif echo "${line}" | grep "^start" > /dev/null ; then
if [[ $newline = "TRUE" ]] ; then
echo >> $outfile
fi
echo "${line}\c" >> $outfile
newline=""
else
echo ", ${line}\c" >> $outfile
newline=""
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:37 AM
03-07-2005 04:37 AM
Re: awk/sed/tr problem
awk '
{
if($1=="start:") { printf("\n%s",$0);}
else { if(length($0) > 1) printf(", %s",$0);}
}
END{printf("\n");}'
Regards
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:40 AM
03-07-2005 04:40 AM
Re: awk/sed/tr problem
as a sh shell script (assuming your data is in a.lis)
#!/usr/bin/sh
while read record
do
if [ -z "$record" ]
then
echo $data
data=""
else
if [ `expr substr "$record" 1 6` = "start:" ]
then
data="$record"
else
data="$data,$record"
fi
fi
done < a.lis
To extract data between reg. expressions use:
sed -n '/reg1/,/reg2/p'
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:49 AM
03-07-2005 04:49 AM
Re: awk/sed/tr problem
cat yourFile | awk '
/^start/ { printf("%s", $0); next;}
NF > 0 {printf(", %s", $0); next;}
{printf("\n"); #blankline
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2005 04:53 AM
03-07-2005 04:53 AM