Re: repeat data at one line and grep as pre-set format
by davido (Cardinal) on Jan 24, 2006 at 08:49 UTC
|
I'm not entirely sure what you're asking for, but it looks like you're just needing to reformat the data to a two-column output. If that's the case, this ought to do the trick:
while( my $line = <DATA> ) {
while( $line =~ m/(\d{8})\s+(\d{5})/g ) {
print "$1 $2\n";
}
}
If I've missed the point to the question, you might help out by providing further explanation on what you are trying to do.
| [reply] [d/l] |
|
perl -pe "s/(\d{8}\s+\d{5})(\s+)/$1\n/g" t.txt
In sure there is a way to do that without needing to copy $1, but I couldnt work it out quickly enough to post.
---
$world=~s/war/peace/g
| [reply] [d/l] |
|
You could avoid copying $1 if you use lookbehind, but there is that pesky problem of \s+ between the digit fields, which won't work with lookbehind (variable width rule). I couldn't tell by the OP's post whether there was a fixed number of spaces or not. Assuming only two spaces (or at least a known quantity of spaces) between the column fields, you could do something like this:
perl -pe "s/(?<=\d{8}\s\s\d{5})\s+/\n/g;" t.txt
| [reply] [d/l] |
|
Re: repeat data at one line and grep as pre-set format
by demerphq (Chancellor) on Jan 24, 2006 at 08:55 UTC
|
perl -aF/\s+/ -lne"print join qq(\t),splice(@F,0,2) while @F" t.txt
Adjust the quotes to suite your shell.
---
$world=~s/war/peace/g
| [reply] [d/l] |
|
| [reply] |
|
| [reply] |
Re: repeat data at one line and grep as pre-set format
by saintmike (Vicar) on Jan 24, 2006 at 08:50 UTC
|
Are you saying you want the first two fields extracted from every line? If so, just use split:
while(<DATA>) {
my @two = split ' ', $_;
print "@two[0,1]\n";
}
__DATA__
00000001 99888 00000011 99889 00000002 99890 00000003 99808
00000021 99888 00000021 99889 00000022 99890 00000023 99900
| [reply] [d/l] |
Re: repeat data at one line and grep as pre-set format
by svenXY (Deacon) on Jan 24, 2006 at 08:51 UTC
|
perl -n -e'print join(" ", (split(/\s+/))[0,1]), "\n"' yourfile.txt
Regards,
svenXY
Update: fixed the line, works now | [reply] [d/l] |
Re: repeat data at one line and grep as pre-set format
by Random_Walk (Prior) on Jan 24, 2006 at 15:15 UTC
|
I think you want to split each pair of data points out to a line on its own instead of having multiple datapoints on one line.
cat yourfile.txt | perl -ple 's/(\S+\s+\S+)\s+/$1\n/g'
should do this for you as long as you have even numbers of datapoints on each line
echo "00000001 99888 00000011 99889 00000002 99890 00000003 998
+08" \
| perl -ple 's/(\S+\s+\S+)\s+/$1\n/g'
00000001 99888
00000011 99889
00000002 99890
00000003 99808
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
| [reply] [d/l] [select] |
|
perl -ple 's/(\S+\s+\S+)\s+/$1\n/g' yourfile.txt
UUOC.
| [reply] [d/l] [select] |
|
| [reply] [d/l] |