Editing 1st or nth column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing 1st or nth column
# 1  
Old 08-02-2009
PHP Editing 1st or nth column

Hi,

I have a file whick is pipe delimited :

100| alpha| tabgo|watch| |||| 444444
| alpha| tabgo|watch| |||| 444444
| sweden |tabgo|watch| |||| 444444
| US| tabgo|watch| |||| 444444
100| factory| tabgo|watch| |||| 444444
| ABC| tabgo|watch| |||| 444444
| launch| tabgo|watch| |||| 444444
| Cam| tabgo|watch| |||| 444444
| Roger| tabgo|watch| |||| 444444
| Sixty| tabgo|watch| |||| 444444

The first line either has a space or a static value 100.

Need to know two things:
1) How to append a character/s say "XXX" in the second column?
Output expected:
100| alphaXX| tabgo|watch| |||| 444444
| alphaXX| tabgo|watch| |||| 444444
| swedenXX |tabgo|watch| |||| 444444
| USXX| tabgo|watch| |||| 444444
100| factoryXX| tabgo|watch| |||| 444444
| ABCXX| tabgo|watch| |||| 444444
| launchXX| tabgo|watch| |||| 444444
| CamXX| tabgo|watch| |||| 444444
| RogerXX| tabgo|watch| |||| 444444
| SixtyXX| tabgo|watch| |||| 444444

2) How to add space for the first column if there is no static value or already has a space
expected output:
100| alphaXX| tabgo|watch| |||| 444444
| alphaXX| tabgo|watch| |||| 444444
| swedenXX |tabgo|watch| |||| 444444
| USXX| tabgo|watch| |||| 444444
100| factoryXX| tabgo|watch| |||| 444444
| ABCXX| tabgo|watch| |||| 444444
| launchXX| tabgo|watch| |||| 444444
| CamXX| tabgo|watch| |||| 444444
| RogerXX| tabgo|watch| |||| 444444
| SixtyXX| tabgo|watch| |||| 444444
# 2  
Old 08-02-2009
Code:
awk  '
  BEGIN {  FS="|"
           OFS="|"
        }
$2 ~ /^[^ ]/ {  # 2nd field 1st char is not space
        $2=sprintf(" %s",$2)
        }
  {  # all
        $2=sprintf("%s%s",$2,"XX")
        print $0
  }

' inputfile

# 3  
Old 08-02-2009
Try....

First one...
Code:
awk 'BEGIN{FS=OFS="|"}{$2=$2"XXX";print $0}' yourfile

Second one...
Code:
awk 'BEGIN{FS=OFS="|"}{if($1!=100)$1=$1" ";print $0}' yourfile

# 4  
Old 08-02-2009
For the second option from malcom: I am gettign awk error
awk: record `100|1||0001|296292323...' has too many fields -- I have 3350 columns in each line separated by delimiter.
Any way the awk messages can be suppressed?

---------- Post updated at 07:27 PM ---------- Previous update was at 07:24 PM ----------

Looks like awk is giving problems for all for too mnay columns everywhere.
# 5  
Old 08-02-2009
in Perl you could...

1. Read in the file line by line -- assume stdin

while (<STDIN>) {
...
}

2. Inside that while() loop, split the line into an array...
@array = split(/\|/); # you need '\' because the pipe is a special character I think

3. Now do what you want with the first two elements of the array...
$array[0] = " " if $array[0] !~ /100/;
$array[1] = $array[1] . 'XXX';

4. Now join it all back together...
$string = join('|', @array);

5. write the string to some file or other structure -- assume STDOUT
print $string . "\n"; # can't forget the lf at end of each string.

When the while() loop ends you will have your original file back with the first two fields modified as desired... The advantage of this approach compared to fancier stuff you could do with more elegant tools (e.g. perl's "map" command) is that this will work even if you have a million (or ten million) lines with a hundred thousand fields each!

execute with 'cat input | perl progname > output'



quine@sonic.net

Last edited by quine; 08-03-2009 at 01:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies

2. UNIX for Dummies Questions & Answers

Getting the lines with nth column non-null

Hi, I have a huge list of archives (.gz). Each archive is about 40MB. A file is generated every minute so if I want to analyze the data for 1 hour I get already 60 files for example. These are text files, ';' separated, each line having about 300 fields (columns). What I need to do is to... (11 Replies)
Discussion started by: Nenad
11 Replies

3. Shell Programming and Scripting

Taking nth column and putting its value in n+1 column using awk

Hello Members, Need your expert opinion how to tackle below. I have an input file that looks like below: USS|AWCC|AFGAW|93|70 USSAA|Roshan TDCA|AFGTD|93|72,79 ALB|Vodafone|ALBVF|355|69 ALGEE|Wataniya (Nedjma)|DZAWT|213|50,550 I like output file in below format: ... (7 Replies)
Discussion started by: umarsatti
7 Replies

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

5. Shell Programming and Scripting

How to remove mth and nth column from a file?

Hi, i need to remove mth and nth column from a csv file. here m and n is not a specific number. it is a variable ex. m=2 n=5 now i need to remove the 2nd and 5th line.. Please help how to do that. Thanks!!! (18 Replies)
Discussion started by: zaq1xsw2
18 Replies

6. Shell Programming and Scripting

Replace the nth column date as MM/DD/YYYY

Hi, I need some unix command to replace the following thing. cat test.dat 1234|test|8/19/2009|8/20/2009|test 1234|test|8/9/2009|8/21/2009|test 1234|test|8/1/2009|8/2/2009|test after processing 1234|test|08/19/2009|08/20/2009|test 1234|test|08/09/2009|08/21/2009|test... (6 Replies)
Discussion started by: anshaa
6 Replies

7. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

Finding Nth Column

Please help me how can I display every nth field present in a "|" delimited file. Ex: If a have a file with data as a|b|c|d|e|f|g|h|k|l|m|n I want to display every 3rd feild which means the output should be c f k n Please help me. (1 Reply)
Discussion started by: ngkumar
1 Replies

10. Shell Programming and Scripting

get 3rd column of nth line

hi; i have a file.txt and its 9th, 10th and 11th line lines are: RbsLocalCell=S2C1 maxPortIP 4 (this is 9th line) RbsLocalCell=S3C1 maxPortIP 4 (this is 10th line) RbsLocalCell=S1C1 ... (11 Replies)
Discussion started by: gc_sw
11 Replies
Login or Register to Ask a Question