The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a new column in a text file snahata Shell Programming and Scripting 10 03-13-2009 07:00 AM
Filtering records of a file based on a value of a column risk_sly UNIX for Dummies Questions & Answers 4 09-24-2008 03:22 AM
Adding specific text and spaces to each line in a text file hertingm Shell Programming and Scripting 4 08-25-2008 03:34 PM
FILE:Adding new column sandeep_hi Shell Programming and Scripting 2 06-09-2006 10:46 AM
Changing the column for a row in a text file and adding another row aYankeeFan Shell Programming and Scripting 9 05-02-2005 10:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2009
rlapate rlapate is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Adding a column to a text based on file name

Dear all,

Does anyone know how I could to add a column of numbers (1s, or 2s, or..., or 6s) to two-column text files (tab-delimited), where the specific number to be added varies as a function of the file naming?

Currently, each of my text files has two columns, so the column with the repeated number in it would be the third column. The groups of six text files are located within separate directories, and the easiest way to get at their naming (which determines which number should be added) is by using "ls" - it will always list them in the right order, such that first text file listed should have a third column added with "1s" (for as many rows as it has; where the number of rows varies depending on the specific text file); and the second text file listed should have a third column appended with "2s", so on and so forth.

I am still quite new to programming, and my intuition tells me I should use the command "ls" and then pipe "|" that to a certain command that would execute "if the file is the first one, add a column of ones, if file is the second one, a column of twos..." etc.

Thank you so much for any feedback!

Regina
  #2 (permalink)  
Old 05-23-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
what have you tried till now??
  #3 (permalink)  
Old 05-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by rlapate View Post
Dear all,

Does anyone know how I could to add a column of numbers (1s, or 2s, or..., or 6s) to two-column text files (tab-delimited), where the specific number to be added varies as a function of the file naming?

Currently, each of my text files has two columns, so the column with the repeated number in it would be the third column. The groups of six text files are located within separate directories, and the easiest way to get at their naming (which determines which number should be added) is by using "ls" - it will always list them in the right order, such that first text file listed should have a third column added with "1s" (for as many rows as it has; where the number of rows varies depending on the specific text file); and the second text file listed should have a third column appended with "2s", so on and so forth.

You don't need ls for that; in fact, it's usually the wrong way to do it. Use filename expansion instead.
Quote:

I am still quite new to programming, and my intuition tells me I should use the command "ls" and then pipe "|" that to a certain command that would execute "if the file is the first one, add a column of ones, if file is the second one, a column of twos..." etc.

No, loop through the files with filename expansion.

Code:
n=1
for file in [whatever pattern works]
do
  awk -v num=$n '{ $(NF+1) = num; print }' "$file" > "$file.new"
  n=$(( $n + 1 ))
done
The awk script might have to be tweaked, depending on the exact format of the files.

Last edited by cfajohnson; 05-23-2009 at 05:21 AM..
  #4 (permalink)  
Old 05-23-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
You might want to do something like

Code:
 i=1;
for file in *.txt; 
do 
awk -F "\t" -v n=$i '{print $0,n}' OFS="\t" $file > $file.tmp;
mv $file.tmp $file; 
i=`expr $i \+ 1 `
done
-Devaraj Takhellambam
  #5 (permalink)  
Old 05-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by devtakh View Post
You might want to do something like

Code:
i=`expr $i \+ 1 `

You don't want to use an external command for simple arithmetic:

Code:
i=$(( $i + 1 ))
  #6 (permalink)  
Old 05-23-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
Quote:
Originally Posted by cfajohnson View Post
You don't want to use an external command for simple arithmetic:

Code:
i=$(( $i + 1 ))
you don't wanna use extra "$" inside
Code:
 
i=$((i+1))
  #7 (permalink)  
Old 05-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by vidyadhar85 View Post
you don't wanna use extra "$" inside
Code:
 
i=$((i+1))

Yes I do. I want my code to be legible.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0