AWK or SED for correct columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK or SED for correct columns
# 1  
Old 04-18-2010
AWK or SED for correct columns

Hello, I have the following file, but one of his columns is not in place, and tried with SED and AWK, how I can correct format?

In the second line break is wrong, and puts it after the first column of next line

Quote:
LECOLAMSTERDAKS COL.MOD.AMSTERDAM K.S LESTE 2,950 8,900 6,325 4,900
LECOLBERLINQS COL MOD.BERLIN Q.S LESTE 1,674 5,350 3,800 2,950LECOLBRUSELASMAT
COL.MOD.BRUSELAS MAT LESTE 1,816 5,500 3,900 2,975
LECOLBRUSELASQS COL.MOD.BRUSELAS Q.S LESTE 2,070 6,275 4,475 3,450
I would appreciate if you could guide me on the subject.
# 2  
Old 04-19-2010
Let's try this one
Code:
awk 'BEGIN{FS=OFS=","}{if(int($NF)!=$NF){sub(int($NF),int($NF)RS);ORS=" "}else{ORS=RS}}1' file

# 3  
Old 04-19-2010
MySQL

Hi
u can try this..

Code:
[root@sistem1lnx ~]# cat dos
LECOLAMSTERDAKS COL.MOD.AMSTERDAM K.S LESTE 2,950 8,900 6,325 4,900
LECOLBERLINQS COL MOD.BERLIN Q.S LESTE 1,674 5,350 3,800 2,950LECOLBRUSELASMAT
COL.MOD.BRUSELAS MAT LESTE 1,816 5,500 3,900 2,975
LECOLBRUSELASQS COL.MOD.BRUSELAS Q.S LESTE 2,070 6,275 4,475 3,450

Code:
pattern=$( (sed '/[0-9][0-9] *$/d' dos | sed 's/\(.*\)\([0-9][,][0-9][0-9]*\)\(.*$\)/\3/') )

Code:
 
patternnew=$pattern

Code:
[root@sistem1lnx ~]# sed -e "s/$pattern/\n&/" dos | sed -e "/[^\$pattern]$/{;N}" -e "s/$patternnew\n/$patternnew /"
LECOLAMSTERDAKS COL.MOD.AMSTERDAM K.S LESTE 2,950 8,900 6,325 4,900
LECOLBERLINQS COL MOD.BERLIN Q.S LESTE 1,674 5,350 3,800 2,950
LECOLBRUSELASMAT COL.MOD.BRUSELAS MAT LESTE 1,816 5,500 3,900 2,975
LECOLBRUSELASQS COL.MOD.BRUSELAS Q.S LESTE 2,070 6,275 4,475 3,450

Regards
Ygemici
# 4  
Old 04-20-2010
Thanks, both ideas are good, I need to study them to understand especially the second, the state generally good, but in a register near the end it does not, but I'll solve it.

They are very nice and I will learn a lot here.

Quote:
VIBECOLPALERMOMA COL.MOD.PALERMO MAT VITOR 1,414 4,250 3,025 2,295
WIRLA7MLSR6332JT LAVADORA MOD.7HLSR-6332 WIRPO 3,600 10,900 7,750 5,675WIRLA7MWT-98825
LAVADORA MOD.7MWT-98825 WIRPO 4,865 14,350 10,100 7,350
# 5  
Old 04-20-2010
Code:
$ cat nitwh
LECOLAMSTERDAKS COL.MOD.AMSTERDAM K.S LESTE 2,950 8,900 6,325 4,900
LECOLBERLINQS COL MOD.BERLIN Q.S LESTE 1,674 5,350 3,800 2,950LECOLBRUSELASMAT
COL.MOD.BRUSELAS MAT LESTE 1,816 5,500 3,900 2,975
LECOLBRUSELASQS COL.MOD.BRUSELAS Q.S LESTE 2,070 6,275 4,475 3,450
VIBECOLPALERMOMA COL.MOD.PALERMO MAT VITOR 1,414 4,250 3,025 2,295
WIRLA7MLSR6332JT LAVADORA MOD.7HLSR-6332 WIRPO 3,600 10,900 7,750 5,675WIRLA7MWT-98825
LAVADORA MOD.7MWT-98825 WIRPO 4,865 14,350 10,100 7,350

$ sed '/ [0-9][,0-9]*$/!{h;s/\(.* [,0-9]*\)\(.*\)/\1/p;g;s//\2/;N;y/\n/ /;}' nitwh    
LECOLAMSTERDAKS COL.MOD.AMSTERDAM K.S LESTE 2,950 8,900 6,325 4,900
LECOLBERLINQS COL MOD.BERLIN Q.S LESTE 1,674 5,350 3,800 2,950
LECOLBRUSELASMAT COL.MOD.BRUSELAS MAT LESTE 1,816 5,500 3,900 2,975
LECOLBRUSELASQS COL.MOD.BRUSELAS Q.S LESTE 2,070 6,275 4,475 3,450
VIBECOLPALERMOMA COL.MOD.PALERMO MAT VITOR 1,414 4,250 3,025 2,295
WIRLA7MLSR6332JT LAVADORA MOD.7HLSR-6332 WIRPO 3,600 10,900 7,750 5,675
WIRLA7MWT-98825 LAVADORA MOD.7MWT-98825 WIRPO 4,865 14,350 10,100 7,350

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Same sed code prints(p) correct, but writtes(w) wrong output

Dear all, I am using sed as an alternative to grep in order to get a specific line from each of multiple files located in the same directory. I am using sed because it prints the lines in the correct order (unlike grep). When I write sed code that prints out the output I get it correct, but... (1 Reply)
Discussion started by: JaNaJaNa
1 Replies

2. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

3. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

4. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

5. Shell Programming and Scripting

Correct a pattern format using sed

hi all I have a file with many lines like aa;bb;cc;dd;ee bb;aa;dd;ee;bb cc;ee;bb;dd;aa ee;cc;bb;aa;dd . . . etc The first line is in right format,Please help me how to use "SED" to change the rest of the lines to the format like line 1 thank all! (1 Reply)
Discussion started by: yuesko
1 Replies

6. Shell Programming and Scripting

Display output as columns using grep/awk/sed

I have several files with say something like cat sparrow I can grep for "cat" and "sparrow" and usually the output is one below the other cat sparrow How can I view these as columns say Pets Birds cat sparrow Would be great if this can be on command line using awk or... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies

9. 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

10. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies
Login or Register to Ask a Question