Re: OT: a problem about text manipulation



On Wed, 1 Nov 2006, Hirofumi Saito wrote:

On Tue, Oct 31, 2006 at 04:18:31PM +0500, Yakov Hrebtov wrote:
Paul Smith wrote:
I am looking for an automatic way of transforming my text file into
another one with the format
You may try this:
cat ./test.txt | awk '{print $1" "$2; print $1" "$3; print $1" "$4}' |
grep -v ' $'

$ awk '{for(i=2;i<=NF;i++)printf $1"\t"$i"\n"}' test.txt

That's all.

or, try this.

$ awk '{for(i=2;i<=NF;i++)print $1, $i}' test.txt

I think awk is the best solution for this problem.

Both of those (as well as Yakov Hrebtov's awk script) generate

1 E
1 E
1 E
2 C
2 D
2 3
2 B
2 E
2 D
4 C
4 E
4 D
5 D
5 E

(notice the line reading '2 3')

instead of what I believe to be the correct

1 E
1 E
1 E
2 C
2 D
3 B
3 E
3 D
4 C
4 E
4 D
5 D
5 E

output.

You both missed that there is a line with multiple different numbered
entries embedded that have to be seperated from each other.

--
Benjamin Franz

"It is moronic to predict without first establishing an error rate
for a prediction and keeping track of one’s past record of accuracy."
-- Nassim Nicholas Taleb, Fooled By Randomness--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list

Relevant Pages

  • Re: General UNIX puzzle
    ... All of those (sed, awk, grep) involve making a copy of the original file ... The gnu shtool is one of the most useful tools for this for in-place ... A Galileo could no more be elected president of the United States than ...
    (freebsd-questions)
  • Re: The smartest band in the world?
    ... I mainly use grep, but I'm ... going to learn awk and sed and improve my bash scripting. ... Oracle accreditation coure, well in the grad courses at least, everything ...
    (alt.guitar)
  • Re: ssh and chroot
    ... ++ echo /bin/mkdir ... ++ awk '' ... ++ grep '^/' ...
    (comp.security.ssh)
  • Re: I need a custom script to extract data from my web logs.
    ... >> You aren't passing anything to awk and you don't need awk anyway. ... My original script was using awk to output the data by ... > piping the output of the original grep through awk to the printf. ... (i.e. searchlogs log???? ...
    (comp.unix.shell)
  • Re: grep switches
    ... grep -n 'pattern' file /dev/null ... grep, so all I need is to the file names and line numbers of the text ... awk '/pattern/ ... But there are some unwanted error ...
    (comp.unix.shell)