Very Simple "Tabs to Newline" Code???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Very Simple "Tabs to Newline" Code???
# 1  
Old 09-22-2010
Data Very Simple "Tabs to Newline" Code???

hi people;

i have a text, iptables.txt and values are as follows inside: (seperators are TAB)

Code:
10.15.2.2  2  40  10.15.3.3  1  10  10.32.2.1  3  40  10.33.6.7  2  50
10.16.3.2  1  60  10.11.3.9  1  20  10.19.2.1  1  80
10.16.9.9  3  20
10.29.7.9  2  90  10.33.2.7  3  40
......

i want to write these values as follows:

Code:
10.15.2.2  2  40
10.15.3.3  1  10
10.32.2.1  3  40
10.33.6.7  2  50
10.16.3.2  1  60
10.11.3.9  1  20
10.19.2.1  1  80
10.16.9.9  3  20
10.29.7.9  2  90
10.33.2.7  3  40
......

after each 3rd value, it must pass to newline. but some lines are 6, some of 9, some are 12 values in iptables.txt Smilie i want to say: "go to new line after each 3 values for each line", but how?? SmilieSmilieSmilieSmilie
# 2  
Old 09-22-2010
Code:
$ ruby -e 'BEGIN{a=File.read("file").split};END{a.each_slice(3) {|x|print "#{x.join(" ")}\n" } }' 
10.15.2.2 2 40
10.15.3.3 1 10
10.32.2.1 3 40
10.33.6.7 2 50
10.16.3.2 1 60
10.11.3.9 1 20
10.19.2.1 1 80
10.16.9.9 3 20
10.29.7.9 2 90
10.33.2.7 3 40

# 3  
Old 09-22-2010
Code:
tr \\t \\n < data | paste - - -

Regards,
Alister

Last edited by alister; 09-22-2010 at 05:20 AM..
# 4  
Old 09-22-2010
Code:
awk '{ i=1;while (i <= NF) {print $i, $(++i), $(++i); ++i}}'

shorter:
Code:
awk '{ i=0; while (i < NF) {print $(++i), $(++i), $(++i)}}'


Last edited by john1212; 09-22-2010 at 05:42 AM.. Reason: added shorter...... and change '(' to '{'
# 5  
Old 09-22-2010
alister;
this is the error:

Code:
sed: -e expression #1, char 18: strings for `y' command are different lengths



---------- Post updated at 11:34 ---------- Previous update was at 11:33 ----------

kurumi;

thanks but this is the error:

Code:
sh: ruby: command not found



---------- Post updated at 11:36 ---------- Previous update was at 11:34 ----------

john1212;

this is the error:

Code:
awk: ( i=1;while (i <= NF) {print $i, $(++i), $(++i); ++i}}
awk:      ^ syntax error
awk: ( i=1;while (i <= NF) {print $i, $(++i), $(++i); ++i}}
awk:                       ^ syntax error
awk: ( i=1;while (i <= NF) {print $i, $(++i), $(++i); ++i}}
awk:                                                ^ syntax error



---------- Post updated at 11:38 ---------- Previous update was at 11:36 ----------


OK john1212 Smilie this is perfectooo :

Code:
awk '{ i=0; while (i < NF) {print $(++i), $(++i), $(++i)}}'

# 6  
Old 09-22-2010
Hi gc_sw,

The sed approach worked fine, but I changed it to a much simpler solution before you posted. Refer to my post above for the paste alternative.

Regards,
Alister

---------- Post updated at 04:40 AM ---------- Previous update was at 04:40 AM ----------

Nevermind. I see now that you did. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Advanced & Expert Users

How newline character "^M" appeared in file?

I have two files. Diff and hexdump tell that they are identical. However vi shows windows newline characters "^M" in one of these files. How is this possible? (2 Replies)
Discussion started by: urello
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Programming

"GLIBC detected Error" for a simple C code.

Here is a simple program to find the vowels in the string. Output is fine. But I'm getting GLIBC detected as shown below : :eek: *** glibc detected *** ./VOWELS: free(): invalid pointer: 0x08b82013 *** ======= Backtrace: ========= /lib/libc.so.6 /lib/libc.so.6(cfree+0x59) ./VOWELS... (5 Replies)
Discussion started by: Ranji Raj
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Match newline character "\n" in lex

Hi everyone! This is my very first post, sorry if I'm not posting in the right category. I'm trying to match a newline "/n" using lex/yacc. For example, print(9,'\n',8) should print 9 8 now do I write a regular expression to match exactly " '\n' " Thanks! (1 Reply)
Discussion started by: code21
1 Replies

7. Shell Programming and Scripting

Help with replacing tabs inside "" with some text/blank

I am poor with scripting;) I have a file in the following format; 'This is a "test in production" of importance.' I want to get rid of the spaces inside the "" part only to get the output as, 'This is a "testinproduction" of importance.' (1 Reply)
Discussion started by: shmathew
1 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

deleting newline characters but not the "true" \n character

hi, i have a file that has about 4500 rows. this was an old microsoft access databse and what i am trying to do is take out the old extra \n newline characters but not take out the "true" newline character. I will explain. i was trying to write a regular expression, but that was not... (1 Reply)
Discussion started by: caddyjoe77
1 Replies
Login or Register to Ask a Question