help with delimiting columns with a space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with delimiting columns with a space
# 1  
Old 03-16-2009
help with delimiting columns with a space

Hi all, i am writting a script to fix some problems we have with data that we need that contains 1000s of records. I have a text file with 3 columns of data. The problem is there should be a space between the end of the first column and the start of the second column. The majority of the data is ok. But as you can see for example 'ADD-SRY_NEES1202106-002' is all one and should have a space after the S and before the 1 the separate the columns of data.

The way I have approched the script is this. The text file data.text contains the data I want to sort out. A second text file called packagesList.txt contains all the possible names of the items in the left column. I go into a while read loop cat the file for each name, then use sed to find and replace with a space at the end.


while read packlist
do
echo $packlist

cat /root/data.txt | grep -w "$packlist" | sed ''s/"'$packlist'"/"$packlist" /'' >> /root/datadelimited.txt

done < /tmp/packagesList.txt

The problem is there is something wrong with the grep because names such as ADD-SRY_NEES and AL ALKGREENS are not being outputed to datadelimited.txt

Any ideas?

Last edited by borderblaster; 03-17-2009 at 12:20 PM..
# 2  
Old 03-17-2009
nawk -f border.awk packagesList.txt data.txt

border.awk:
Code:
FNR==NR { pack[$0]; next }
NF != 3 {
   for (i in pack)
    if (match($1, "^" i)) {
       $1 = substr($1, 1, RSTART+RLENGTH-1) OFS substr($1, RSTART+RLENGTH)
       break
    }
}
1


Last edited by vgersh99; 03-17-2009 at 09:44 AM..
# 3  
Old 03-17-2009
Hi vgersh that seems to work well. There is a problem however if the package contains a space e.g. AL ALKGREENS it does not match and doesnt put a space after?
# 4  
Old 03-17-2009
Then you already have 3 fields - you're using space as a field separator - how would you distiguish fields with embedded spaces then?
# 5  
Old 03-17-2009
thats the problem I have vgersh, some package names have spaces in them and the field separator is a space also. the file called packs.txt contains the names of the packages, is there not some way to do a regex that will match the names of the packs exactly?
# 6  
Old 03-17-2009
how about this:
Code:
FNR==NR { pack[$0]; next }
{
   for (i in pack)
    if (match($0, "^" i) && !($1 in pack)) {
       $0 = substr($0, 1, RSTART+RLENGTH-1) OFS substr($0, RSTART+RLENGTH)
       break
    }
}
1

# 7  
Old 03-17-2009
vgersh that works perfectly! your a genious
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to print columns with space

Hello I am trying to pull specific columns from an output file that contains spaces and make it a variable.. Here is a piece of the output file and my command: Host1 UNIX /vol/volume/my stuff When I pull in the data into my variable the word stuff is left off - I need this as part of my... (10 Replies)
Discussion started by: fmalvest
10 Replies

2. Shell Programming and Scripting

Insert space in specific column among many columns

Hello, I have some problem in inserting the space for the pairs of columns. I have the input file : I used this code below in replacing it using space in specific column (replace space in each two columns) sed -e "s/,/ /2" -e "s/,/ /3" inputfile Output showed : However, I have many... (3 Replies)
Discussion started by: awil
3 Replies

3. Shell Programming and Scripting

Romove columns and replace a space with a character

Hi, I have a file containing this: testvol1 unix enabled testvol2 unix enabled testvol3 unix enabled testvol3 qtree1 unix enabled testvol3 qtree2 unix enabled testvol4 unix enabled testvol4 qtree1 unix enabled And I want an output of this: testvol1... (4 Replies)
Discussion started by: niap21
4 Replies

4. Shell Programming and Scripting

delimiting using sed command

Hi, I have an output after performing a grep: FAN and i did awk '{print $2}' it shows: is there a way, by using sed that it will only show output as NO_FAULT without the brackets ? i tried doing sed 's/^*//' but the output is NO_FAULT] with the ] at the... (12 Replies)
Discussion started by: mosies
12 Replies

5. Shell Programming and Scripting

Columns to rows with space

Hi, My input is in the following way a b c d e f I would like to have it printed as a b c d e f Any awk scripts are appreciated. (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. UNIX for Dummies Questions & Answers

help needed: remove space between certain columns

dear all, i have a data looks like this (i have 3000 columns): rs123 A T T G C C C C C C A A A A A A A A A A A ... rs154 T T G C C C C C A A A A A A A A A A T T G ... rs126 A C C C C C A A A A A A A A A A A A T T G ... I want to remove all the space after the 2nd column and make the... (2 Replies)
Discussion started by: forevertl
2 Replies

7. Shell Programming and Scripting

AWK- delimiting the strings and matching the fields

Hello, I am newbie in awk. I have just started learning it. 1) I have input file which looks like: {4812 4009 1602 2756 306} {4814 4010 1603 2757 309} {8116 9362 10779 } {10779 10121 9193 10963 10908} {1602 2756 306 957 1025} {1603 2757 307} and so on..... 2) In output: a)... (10 Replies)
Discussion started by: kajolo
10 Replies

8. HP-UX

How to add space in between columns in VI editor

Hi, I am trying to add space after 3rd and 6th column for editing a file in special format. How should I achieve in VI?. So, is it possible to do in it in sed ? thanks vipa (1 Reply)
Discussion started by: vipas
1 Replies

9. Shell Programming and Scripting

Trying to space columns in a report

I have a program that calculates shipping cost for an item and prints the information to a file. I cannot seem to get the lines to print with equal spacing in columns. I need data like this to be aligned into columns: Optical Mouse 5 A $25 Compaq Presario 3... (2 Replies)
Discussion started by: turbulence
2 Replies

10. Shell Programming and Scripting

AWK delimiting

I have a directory of files DATA1,DATA2,DATA3, etc... each file has 1 column of data Example: File DATA1 name date time time requested approved I need to change these to CSV files so I can import the data. I believe AWK is what i need but I'm new to AWK and can't seem to get the... (5 Replies)
Discussion started by: barrro
5 Replies
Login or Register to Ask a Question