Add column to a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Add column to a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-05-2011
Olivia Olivia is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 3 February 2012, 1:00 AM EST
Posts: 27
Thanks: 11
Thanked 0 Times in 0 Posts
Add column to a file

Hola,
How can I add a column to a existing file??

PS: The column which should be added need to be the first column and it will be a parameter from the script.
Example:
1 name1
2 name2
3 name3
4 name3

Need to add parameter $file as a first column. $file is a file name with time stamp which will be same for all the rows.

Thanks
Olivia
Sponsored Links
    #2  
Old 10-05-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 April 2014, 6:29 PM EDT
Location: Saskatchewan
Posts: 18,531
Thanks: 681
Thanked 3,036 Times in 2,856 Posts
I'm not sure if that's your input data, or your output data.

We really need to see both to understand what you want.
Sponsored Links
    #3  
Old 10-05-2011
aster007 aster007 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 18 November 2013, 1:01 PM EST
Location: Richmond, Virginia
Posts: 46
Thanks: 14
Thanked 9 Times in 7 Posts

Code:
file=<filename with timestamp>

while read LINE
do 
    echo "$file $LINE" >> temp_file.txt
    
done < input_file 

temp_file.txt > input_file

Quote:
where
input_file is

1 name1
2 name2
3 name3
4 name3


which after code should be

<filename with timestamp> 1 name1
<filename with timestamp> 2 name2
<filename with timestamp> 3 name3
<filename with timestamp> 4 name3
As Corona688 has said, we will only get a better idea of what you want if you post more details.
This is what I roughly understood from your post.
    #4  
Old 10-05-2011
Olivia Olivia is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 3 February 2012, 1:00 AM EST
Posts: 27
Thanks: 11
Thanked 0 Times in 0 Posts
Apologize, I was not clear

That's is input
my output should be

$File 1 name1
$File 2 name2
$File 3 name3
$File 4 name4


$File is the file name generated from the script.

---------- Post updated at 04:17 PM ---------- Previous update was at 04:10 PM ----------

Thank you both yeah, thats exactly what I am looking for i appreciate your help.

Quote:
Originally Posted by aster007 View Post
Code:
file=<filename with timestamp>

while read LINE
do 
    echo "$file $LINE" >> temp_file.txt
    
done < input_file 

temp_file.txt > input_file



As Corona688 has said, we will only get a better idea of what you want if you post more details.
This is what I roughly understood from your post.
---------- Post updated at 04:57 PM ---------- Previous update was at 04:17 PM ----------

Yeah it worked but is there any other way as because it's been adding first column to the blank rows too
Sponsored Links
    #5  
Old 10-05-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 April 2014, 6:29 PM EDT
Location: Saskatchewan
Posts: 18,531
Thanks: 681
Thanked 3,036 Times in 2,856 Posts
Quote:
Originally Posted by Olivia View Post
Yeah it worked but is there any other way as because it's been adding first column to the blank rows too
Show us a representative sample of your input, then, and a representative sample of your output.
Sponsored Links
    #6  
Old 10-06-2011
jayan_jay's Avatar
jayan_jay jayan_jay is offline Forum Advisor  
Forum Advisor
 
Join Date: Jul 2008
Last Activity: 2 December 2013, 5:52 PM EST
Posts: 832
Thanks: 9
Thanked 185 Times in 176 Posts
for your previous post ..

Code:
$ awk '{print "filename"$0}' infile

As Corona said, post sample i/p and o/p content to proceed further ..
Sponsored Links
    #7  
Old 10-06-2011
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 20 April 2014, 10:20 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,632
Thanks: 24
Thanked 177 Times in 161 Posts
Hi.
Quote:
To obtain the best answers quickly when you want help in
processing datasets -- extracting, transforming, filtering,
you should:

1. Post representative samples of your data (i.e. data that
should "succeed" and data that should "fail")

2. Post what you expect the results to be, instead of trying
only to describe them. Be clear about how the results are to be
obtained, e.g. "add field 2 in file1 to field 3 from file2"

3. Post what you have attempted to do so far.

4. Place the data and expected output within CODE tags, so that
they are more easily readable.

Special cases, exceptions, etc., are very important to include
in the samples.
cheers, drl
The Following User Says Thank You to drl For This Useful Post:
Franklin52 (10-06-2011)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing column of two different files and print the column from in order of 2nd file CAch Shell Programming and Scripting 14 08-24-2011 01:45 PM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 10:28 AM
creating a file using the fist column and printing second column viralnerd UNIX for Dummies Questions & Answers 5 12-16-2009 11:05 PM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM



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