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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append a specific keyword in a text file into a new column
# 1  
Old 03-27-2016
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.
Code:
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 be tranaformed into first column of the file.
Code:
03/25/2016,A,B,C
03/25/2016,D,E,F
03/26/2016,1,2,3
03/26/2016,4,5,6
03/27/2016,6,4,3
03/27/2016,4,5,6

Currently I am manually doing this process in Excel but I am finding it as a very tedious process.

Thanks in advance
# 2  
Old 03-27-2016
Can be done in EXCEL pretty simple and fast. However, try
Code:
awk '/^date/ {T = $2; next} {print T, $0}' OFS="," file1
03/25/2016,A,B,C
03/25/2016,D,E,F
03/26/2016,1,2,3
03/26/2016,4,5,6
03/27/2016,6,4,3
03/27/2016,4,5,6

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-27-2016
Code:
perl -nle 'm{((\d+/){2}\d+)} and $d=$1 or print "$d,$_"' ks_reddy.csv
03/25/2016,A,B,C
03/25/2016,D,E,F
03/26/2016,1,2,3
03/26/2016,4,5,6
03/27/2016,6,4,3
03/27/2016,4,5,6


Last edited by Aia; 03-27-2016 at 04:31 PM..
This User Gave Thanks to Aia For This Post:
# 4  
Old 03-28-2016
Thanks RudiC and Aia for your prompt replies.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to cut from a text file based on value of a specific column?

Hi, I have a tab delimited text file from which I want to cut out specific columns. If the second column equals one, I want to cut out columns 1 and 5 and 6. If the second column equals two, I want to cut out columns 1 and 5 and 7. How do I go about doing that? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

2. UNIX for Dummies Questions & Answers

Switching the values in a specific column of a text file

Hi, I have a space de-limited text file. In the fifth column, I would like to switch "1"s with "2"s. How do I go about doing that? Thanks! Sample input: 0 311000259 0 0 1 1 0 311000397 0 0 1 2 0 311000491 0 0 2 1 0 311000516 0 0 2 1 0 311000541 0 0 1 1 0 311000558 0 0 2 1 0 311000566 0... (1 Reply)
Discussion started by: evelibertine
1 Replies

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

4. UNIX for Dummies Questions & Answers

Adding tags to a specific column of a space delimited text file

I have a space delimited text file with two columns. I would like to add NA to the first column of the text file. Input: 19625 10.4791768259 19700 10.8146489183 19701 10.9084026759 19702 10.9861346978 19703 10.9304364984 Output: NA19625 10.4791768259 NA19700 10.8146489183... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

7. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

9. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question