Append a new column in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append a new column in a file
# 1  
Old 11-01-2011
Append a new column in a file

Hi all,

I need to append 'ls --' command output as a new column in a existing file.

for example,
when i do ls -- i will get the below,
Code:
 
aaa_import.csv
bbb_import.csv
ccc_import.csv
ddd_import.csv
ffff_import.csv

i need to cut the aaa, bbb, ccc, ddd and append this a new column to a respective file

aaa should be added as new last column in aaa_import.csv file and bbb in bbb_import.csv file...

i tired upto this,
Code:
 
ls --|cut -d"_" -f 1

and i got the out put below,
Code:
aa
bbb
ccc
ddd

i don't knw how to apppend as new cloumn .

Please help me on this.

Thanks,
Baski.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 11-01-2011 at 09:59 AM..
# 2  
Old 11-01-2011
Try this:
Code:
appendValue="APPEND"
ls -1 | cut -d"_" -f 1 | \
while read val
do
     echo "${val} ${appendValue}"
done

# 3  
Old 11-01-2011
THanks,


But how it will append the cut command ouput as a last column in respective file,

for example,
when i do 'ls -1' . i will get the below,

Code:
Code: ls -1
aaa_import.csv
bbb_import.csv
ccc_import.csvddd_import.csv
ffff_import.csv

i need to cut the aaa, bbb, ccc, ddd and append this a new column to a respective file

aaa should be added as new last column in aaa_import.csv file and bbb in bbb_import.csv file...

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 11-01-2011 at 10:31 AM..
# 4  
Old 11-01-2011
Code:
$ ls -1 *.csv | nawk -F_ '{print "echo "$1" >> "$0}' | sh

# 5  
Old 11-01-2011
This one will append the filename part to all lines in the file:
Code:
ls -1 | \
while read fname
do
	appendValue=`echo "${fname}" | cut -d"_" -f1`
	sed -i 's/.*/& '"${appendValue}"'/g' "${fname}"
done

# 6  
Old 11-01-2011
Here's yet another way. You didn't mention what platform you're working on so these commands may not be available:
Code:
ls *import* | cut -c1-3 > file2.txt && pr -m -t -s file1.txt file2.txt | gawk '{print $1"\t"$2"\t"$3}' | sponge file1.txt

The 'ls' command is dumped to file2.txt. The 'pr' command merges the contents of file1.txt (the existing file) and file2.txt. Then we use 'gawk' to display the results, and lastly the sponge utility will allow writing to the same existing file (file1.txt - NOTE: normally this isn't possible, but the sponge utility allow this action.)

file1.txt (before)
Code:
123     456 
789     000 
333     222 
oop     ppo
889     09a

file2.txt
Code:
aaa
bbb
ccc
ddd
fff

file1.txt (after)
Code:
123	456	aaa
789	000	bbb
333	222	ccc
oop	ppo	ddd
889	09a	fff


Last edited by Franklin52; 11-02-2011 at 04:14 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

3. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

4. Shell Programming and Scripting

Append a specific keyword in a text file into a new column

All, I have some sample text file(.csv) in the below format. In my actual file there are at least 100K rows. date 03/25/2016 A,B,C D,E,F date 03/26/2016 1,2,3 4,5,6 date 03/27/2016 6,4,3 4,5,6 I require the following output where in the date appeared at different locations need to... (3 Replies)
Discussion started by: ks_reddy
3 Replies

5. Shell Programming and Scripting

Match value in column and append file with new values

Hi, I need help to match two files based on two columns. file_1 ID AA An Ca Ele Pro Su Ot Tra g13950 No No Yes No Yes Yes Yes Yes g05760 Yes No No No No Yes Yes Yes g12640 No No No No No No No No k17720 No Yes No No No No No Yes g05640 Yes Yes Yes No No Yes Yes Yes file_2 ... (8 Replies)
Discussion started by: redse171
8 Replies

6. Shell Programming and Scripting

Append sha256 column to a file

Hi, My file looks like below: 1 abc@xyz.com 2 klm@klm.com There are two fields separated by tab. i want to append a third field which will be the sha256 hash of the email address field. Anyone have any solution? Note: I have already tried with while read line .... but the... (1 Reply)
Discussion started by: Mihir Ray
1 Replies

7. Shell Programming and Scripting

Append data to first column delimited file

Hi, I have a data like Input: 12||34|56|78 Output: XYZ|12||34|56|78 I tried like this , but it puts it on another line awk -F "|" ' BEGIN {"XYZ"} {print $0} 'file Any quick suggessitons in sed/awk ? am using HP-UX (3 Replies)
Discussion started by: selvankj
3 Replies

8. UNIX for Dummies Questions & Answers

Append a line to single column text file

I would like to add a line to the end of a single column text file. How do I go about doing that? Input: BEGIN 1 2 3 Output: BEGIN 1 2 3 END Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. UNIX for Dummies Questions & Answers

add (append) a column in a tab delimited file

I have a file having the following entries: test1 test2 test3 11 22 33 22 44 66 99 99 44 --- I want to add a column so that the above file becomes: test1 test2 test3 notest 11 22 33 * 22 44 66 * 99 99 44 * --- Thanks (6 Replies)
Discussion started by: mary271
6 Replies

10. Shell Programming and Scripting

How to append database column to a delimited file

Hi, I have the below flat filewith ~ as delimiter emp.no~dept.name I need to append corresponding emp.name column which will come from database based on emp.no in the flat file. I need the output as dept.name~emp.name Can anyone please help me in resolving this issue.. I tried the... (2 Replies)
Discussion started by: siri_886
2 Replies
Login or Register to Ask a Question