Unix Shell - horizontal separated record to vertical?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > horizontal separated record to vertical?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author horizontal separated record to vertical?
key9

2006-12-15, 7:25 pm

Hi All

I have a file, with format
record1 record2 record3 record4 record5.......

I want to change them into
record1
record2
record3
record4
record5
........


so I have ideas:
read the file into an array an print them
how to express that in bash?

but I don't think it is simplest, maybe the best way is replace each
"separat char" into "new line"
and how to express that?



thank you very much!

your key9



Janis Papanagnou

2006-12-16, 1:31 am

key9 wrote:
> Hi All
>
> I have a file, with format
> record1 record2 record3 record4 record5.......
>
> I want to change them into
> record1
> record2
> record3
> record4
> record5
> .......
>
>
> so I have ideas:
> read the file into an array an print them
> how to express that in bash?
>
> but I don't think it is simplest, maybe the best way is replace each
> "separat char" into "new line"
> and how to express that?


So you've just a single line and want a single column.

If the separate char is a space...

tr " " "\n" <yourfile


Janis

>
>
>
> thank you very much!
>
> your key9
>
>
>

Ed Morton

2006-12-16, 1:31 am

key9 wrote:

> Hi All
>
> I have a file, with format
> record1 record2 record3 record4 record5.......
>
> I want to change them into
> record1
> record2
> record3
> record4
> record5
> .......
>
>
> so I have ideas:
> read the file into an array an print them
> how to express that in bash?
>
> but I don't think it is simplest, maybe the best way is replace each
> "separat char" into "new line"
> and how to express that?


In awk, et the input Record Separator to a space and rely on the default
Ouput Record Separator being a newline:

$ echo "record1 record2 record3 record4 record5" | awk -v RS=' ' 1
record1
record2
record3
record4
record5

Regards,

Ed.
key9

2006-12-16, 1:31 am

>
> So you've just a single line and want a single column.
>
> If the separate char is a space...
>
> tr " " "\n" <yourfile
>


It works and also I want know
How to express this in awk 's prattern?
In case the file is not only single line?


Janis Papanagnou

2006-12-16, 1:31 am

key9 wrote:
>
>
> It works and also I want know
> How to express this in awk 's prattern?


Ed already answered that case for awk.

> In case the file is not only single line?
>
>


Read in the file into a matrix (2-dimensional array) line-by-line and
at the end access the elements with indexes switched. For example...

{ for(f=1;f<=NF;f++)a[NR,f]=$f }
END{ for(c=1;c<=NF;c++)for(l=1;l<=NR;l++)
printf("%s%s",a[l,c],(l!=NR)?" ":"\n") }

It requires to read the whole data file into the array.

Janis
Chris F.A. Johnson

2006-12-16, 1:31 am

On 2006-12-16, key9 wrote:
> Hi All
>
> I have a file, with format
> record1 record2 record3 record4 record5.......
>
> I want to change them into
> record1
> record2
> record3
> record4
> record5
> .......
>
>
> so I have ideas:
> read the file into an array an print them
> how to express that in bash?
>
> but I don't think it is simplest, maybe the best way is replace each
> "separat char" into "new line"
> and how to express that?


Any of the following:

printf "%s\n" $(< FILENAME)

tr ' ' '\012' < FILENAME

sed 's/ /\
/g' FILENAME

awk -vOFS="\n" '{$1=$1; print }' FILENAME


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
key9

2006-12-16, 7:26 am

> printf "%s\n" $(< FILENAME)

could you explain this?


Chris F.A. Johnson

2006-12-16, 1:19 pm

On 2006-12-16, key9 wrote:
>
> could you explain this?


man bash:

Command Substitution
Command substitution allows the output of a command to replace the com-
mand name. There are two forms:


$(command)
or
`command`

Bash performs the expansion by executing command and replacing the com-
mand substitution with the standard output of the command, with any
trailing newlines deleted. Embedded newlines are not deleted, but they
may be removed during word splitting. The command substitution $(cat
file) can be replaced by the equivalent but faster $(< file).




--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com