Split line in 4 parts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split line in 4 parts
# 1  
Old 03-08-2016
Split line in 4 parts

Hi Guys,

I have file A.txt

Code:
1
2
3
4
5
6
7
8
9
10
11

Want Output :-

Code:
1 2 3
4 5 6
7 8 9
10 11

I have tried Below Code but Only work for first few lines.
Code:
sed '1,3d;N;s/\n/ /'

Thanks
# 2  
Old 03-08-2016
Hello pareshkp,

Following may help you in same.
1st: By using xargs as follows.
Code:
xargs -n3 <  Input_file

Output will be as follows.
Code:
1 2 3
4 5 6
7 8 9
10 11

2nd: By using awk solution.
Code:
awk '{ORS=NR%3==0?"\n":" "} 1'  Input_file

Output will be as follows.
Code:
1 2 3
4 5 6
7 8 9
10 11

Hope this helps you.

Thanks,
R. Singh
# 3  
Old 03-08-2016
With the awk suggestion there may be a missing newline at the end, rendering the output an improper Unix format. This could be corrected:
Code:
awk 'END{if(NR%3) printf RS} {ORS=NR%3?FS:RS}1'

--
And for completeness, the inevitable :
Code:
paste -d" " - - - < file

is another one
# 4  
Old 03-09-2016
Or with:
bash code:
  1. #!/bin/bash
  2. STR="1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11"
  13.  
  14. NR=2 ## 0, 1 , 2 = 3 items in a row
  15. C=0
  16.  
  17. for n in $STR;do
  18.     if &#91; $C -eq $NR ]
  19.     then    printf "%s\n" "$n"
  20.     else    printf "%s " "$n"
  21.     fi
  22.     ((C++))
  23.     &#91; $C -gt $NR ] && C=0
  24. done
  25. printf "\n"
  26. exit 0

hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split the all files in to 8 parts in a folder

Hi, I have different files and i need to split the files in that folder split in to 8 parts with equal number of lines....! any fastest way of doing this in awk. for an example i have a file called "BillingDetails_BaseFile.csv" with total line count 65536 and i need to split in to 8 parts... (1 Reply)
Discussion started by: Raghuram717
1 Replies

2. Shell Programming and Scripting

Extract parts of the line

I have a long list of lines in a txt file which i'm only interested to extract the list of domains like the colored ones. domain.com domain.com/page codes $.09 domain.org domain.org/page2/ codes $0.10 domain.net domain.net/page03 codes $0.05 domain.info ... (3 Replies)
Discussion started by: garfish
3 Replies

3. UNIX for Dummies Questions & Answers

Split a file into parts only if the first field is different

Hi, I have a file like this: aaa 123 aaa 223 aaa 225 bbb 332 bbb 423 bbb 6755 bbb 324 ccc 112 ccc 234 ccc 897 Which I need to split into several files, something like split -l 3 but the way that the lines with the same names would only go into one file: (7 Replies)
Discussion started by: coppuca
7 Replies

4. UNIX for Dummies Questions & Answers

How To Split A File In Two Rar Parts?

I Am connected to Whatbox.ca Seed Box Via SSH!! i have a file named avicii.mp3. I Want to split it into two rar parts as Apart1.rar and Apart2.rar So That When i Download Both the parts to My PC And Extract Them They Come out As Whole Avicii.mp3. There is also one more problem!! When I Rar A... (18 Replies)
Discussion started by: anime12345
18 Replies

5. Shell Programming and Scripting

Split file into n parts.

Hi all: I have a 5-column tab-separated file. The only thing that I want to do with it is to split it. However, I want to split it with a 80/20 proportion -- randomized, if possible. I know that something like : awk '{print $0 ""> "file" NR}' RS='' input-file will work, but it only... (6 Replies)
Discussion started by: owwow14
6 Replies

6. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

7. Shell Programming and Scripting

Format Parts of Multiline Section to Single Line

Hello, I have an input file that I need formatted. I was hoping I could use bash to get this done. Title: Kitchen Blender Washer Dishes Title: Bathroom Toilet Sink Title: Bedroom Bed Desired output would be similar to Results("Blender","Washer","Dishes") (1 Reply)
Discussion started by: jl487
1 Replies

8. Shell Programming and Scripting

[ask]break line number into several parts

hlow all, i have file with wc -l file.txt is 3412112 line number so I want to break these files into several parts with assumsi line 1-1000000 will be create part1.txt and 1000001-2000000 will create part2.txt and 2000001-3000000 will create part3.txt and 3000001-3412112 will create... (5 Replies)
Discussion started by: zvtral
5 Replies

9. Shell Programming and Scripting

deleting certain parts of a line in perl

I have a filr data.txt. Its contents are Available labels (* indicates activated, I indicates installed, R idicates running): abc-3.0.3 def-3.0.4 xyz-3.1.2-1.0 I want to delete " Available labels (* indicates activated, I indicates installed, R idicates running):" and... (3 Replies)
Discussion started by: lassimanji
3 Replies

10. UNIX for Dummies Questions & Answers

removing parts of a line with SED

hi, i'm trying to erase all the characters after, and including, the first test test Output: test1 test2 test3 this is what I tried, but didn't work sed "s/*//" file > testfilename any suggestions? thanks, gammmaman (2 Replies)
Discussion started by: gammaman
2 Replies
Login or Register to Ask a Question