the easiest way to break down this column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting the easiest way to break down this column?
# 1  
Old 08-27-2010
the easiest way to break down this column?

i have a one column txt file, which has a large amount of data which look like this

a
11
3
b
45
77
r
7
9
blah
blah
blah

what i am trying to do here is for every 3 lines, i want to move the 2nd and 3rd line to the first line, so it will look like this

a 11 3
b 45 77
r 7 9
blah blah blah

what is the easiest way to make this happen?
# 2  
Old 08-27-2010
Code:
awk -vORS=" " '!(NR%3){printf $0"\n";next}1' file

# 3  
Old 08-27-2010
hmmm, i got the following error

awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 08-27-2010
What system are you using?
# 5  
Old 08-27-2010
solaris, and i just tested on ubuntu and it works! Smilie

if it is possible can you elaborate a little bit what your awk script does?

Quote:
Originally Posted by bartus11
What system are you using?
# 6  
Old 08-27-2010
Quote:
Originally Posted by fedora
solaris, and i just tested on ubuntu and it works! Smilie

if it is possible can you elaborate a little bit what your awk script does?
-vORS=" " - sets record (line) separator from newline to space
!(NR%3){printf $0"\n";next} - every 3 lines print current line followed by newline
1 - print current line
This User Gave Thanks to bartus11 For This Post:
# 7  
Old 08-27-2010
Hi.

An easy way is to use paste:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate easy collapse with paste.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
version >/dev/null 2>&1 && version "=o" bash paste

pl " Data file data1:"
cat data1

pl " Results of paste:"
paste - - - < data1

exit 0

producing:
Code:
$ ./s1
OS, ker|rel, machine: SunOS, 5.10, i86pc
GNU bash 3.00.16
paste - ( /usr/bin/paste Jan 22 2005 )

-----
 Data file data1:
a
11
3
b
45
77
r
7
9
blah
blah
blah

-----
 Results of paste:
a       11      3
b       45      77
r       7       9
blah    blah    blah

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this (1 Reply)
Discussion started by: tmonk1
1 Replies

2. Shell Programming and Scripting

Break Column nth in a CSV file into two

Hi Guys, Need help with logic to break Column nth in a CSV file into two for e.g Refer below the second column as the nth column "abcd","","type/beta-version" need output in a following format "abcd","/place/asia/india/mumbai","/product/sw/tomcat","type/beta-version" ... (5 Replies)
Discussion started by: awk-admirer
5 Replies

3. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

4. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this http://unix.com/abc/def http://unix.com/kil/min I want to use the / as separator and print the last content as another column like this http://unix.com/abc/def def http://unix.com/kil/min min I was using awk -F option and then joining the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Ubuntu

What is the easiest way to install Ubuntu?

I am planning to dual boot Windows XP and Ubuntu 9.04, I have already burned Ubuntu into a CD and have also downloaded the ISO. But when I go to My Computer to start the installation, a different icon appears. And when I open it with ISO Buster, it opens up separately. I'm confused on what to... (5 Replies)
Discussion started by: Anna Hussie
5 Replies

6. Programming

SQL for table with column (varchar2 2000) and line break in it

Hi, I need a sql statement for a table, which simply stores a text. It has a column ID, key1, key2, ..., text, date etc. The text can be entered using a line break (return) in an oracle form. ID key1 key2 text date 1 K1 ... (16 Replies)
Discussion started by: spidermike
16 Replies

7. UNIX for Dummies Questions & Answers

Which Certification Is Easiest/Quickest?

I need to gain one of these fast!... Any ideas on best way to go about it?...I have money to spend but not sure how to do it. Solaris certification (SUN Admin Part 1 and/or Part 2) as well as proficiency in AIX OR AIX certification (ASA and/or ASP) as well as proficiency in Solaris, (1 Reply)
Discussion started by: IBMPBC
1 Replies

8. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies

9. UNIX for Advanced & Expert Users

Suggest me the easiest method

Hi, I want to check whether a file of the format myfile_YYYYMMDD_HHMMSS.txt exists in a particular directory. Here YYYYMMDD_HHMMSS is the time stamp, so it will be numbers always . What is the best method to do this I did it like this : ls myfile_*_*.txt but it will list files... (1 Reply)
Discussion started by: shihabvk
1 Replies

10. UNIX for Dummies Questions & Answers

Easiest and best scripting book ?

Hello, I am in need of a easy to read and easy to understand, Unix scripting book. Can anyone make any recommendations ? thanks in advance simon2000 (2 Replies)
Discussion started by: simon2000
2 Replies
Login or Register to Ask a Question