Adding column in a .txt file


 
Thread Tools Search this Thread
Operating Systems AIX Adding column in a .txt file
# 1  
Old 10-08-2010
Network Adding column in a .txt file

Helle,
I want to create a .ksh script in order to realize the following :
I have a .txt file organized in a bloc of information, each bloc start with 000 as following:

Code:
000...
001...
003...
004...
000...
001...
003...
004...
.
.

My aim is to add a new column at the end of each line in order to have a unique key for each bloc as following:
Code:
000...1
001...1
003...1
004...1
000...2
001...2
003...2
004...2

.
.
thanks for your help Smilie!

Last edited by radoulov; 10-08-2010 at 06:40 AM.. Reason: Code tags, please!
# 2  
Old 10-08-2010
If awk is acceptable:

Code:
awk '$0 = $0 (/^000/ ? ++_ : _)' infile


Last edited by radoulov; 10-08-2010 at 07:10 AM.. Reason: Corrected.
# 3  
Old 10-08-2010
Another one Smilie:
Code:
awk '/^000/{ORS=++c RS}1' file


Last edited by Franklin52; 10-08-2010 at 07:16 AM.. Reason: Typo: Replace last " with '
# 4  
Old 10-08-2010
Quote:
Originally Posted by Franklin52
Another one Smilie:
Code:
awk '/^000/{ORS=++c RS}1" file

Smilie Nice one!

---------- Post updated at 12:16 PM ---------- Previous update was at 12:10 PM ----------

Based on Franklin52's code:

Code:
awk 'ORS=(/^000/?++_:_)RS' infile

# 5  
Old 10-08-2010
Quote:
Originally Posted by radoulov
If awk is acceptable:

Code:
awk '$0 = $0 (/^000/ ? ++_ : _)' infile

This is nice too! Smilie
# 6  
Old 10-08-2010
thank you for your help Smilie,
i find that the command awk is very powerfulSmilie ! do you have a tutorial that resume different use of this command?
# 7  
Old 10-08-2010
Quote:
Originally Posted by zainab2006
thank you for your help Smilie,
i find that the command awk is very powerfulSmilie ! do you have a tutorial that resume different use of this command?
Some links:

Awk - A Tutorial and Introduction - by Bruce Barnett

http://www.gnu.org/manual/gawk/gawk.pdf
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Phrase txt file row to column

Hi Guys, I have one Big txt file and i what to phrase specific part as below. Input :- Event Event { recordLength 160118, recordType 411, eventId 3102118, INTERNAL_PER_RO_ME_TA { EVVXX_TIMESTAMP_HOUR 16, EVVXX_TIMESTAMP_MINUTE 15, EVVXX_TIMESTAMP_SECOND 3, ... (6 Replies)
Discussion started by: pareshkp
6 Replies

2. Shell Programming and Scripting

Adding Extra Column in txt file base on Condition

HI Guys, I have below input. Output Base on Below Condition. 1> if forth column is empty and next coming line have same name with \es then add that column name on all rows 2>rest of all are es:vsDataEUtranCellFDD Input:- CCL01736 CCL01736_7A_1 es:vsDataEUtranCellFDD ... (3 Replies)
Discussion started by: pareshkp
3 Replies

3. UNIX for Dummies Questions & Answers

Add a new column to txt file containing filename

I would like help adding a new column to a large txt file (~10MB) that contains the filename. I have searched other posts but have not found an adequate solution. I need this extra column so I can concatenate >100 files and perform awk searches on this large file. My current txt file look... (4 Replies)
Discussion started by: kellywilliams
4 Replies

4. UNIX for Dummies Questions & Answers

Sorting a txt file that is a single column

How do you sort a text file that is made up of a single column? (sorting done in alphabetical order) Example input: MAP1S ISYNA1 STAT6 Example output: ISYNA1 MAP1S STAT6 Double post (0 Replies)
Discussion started by: evelibertine
0 Replies

5. UNIX for Dummies Questions & Answers

Sorting a txt file that is a single column

How do you sort a text file that is made up of a single column? (sorting done in alphabetical order) Example input: MAP1S ISYNA1 STAT6 Example output: ISYNA1 MAP1S STAT6 (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

merge two two txt files into one file based on one column

Hi, I have file1.txt and file2.txt and would like to create file3.txt based on one column in UNIX Eg: file1.txt 17328756,0000786623.pdf,0000786623 20115537,0000793892.pdf,0000793892 file2.txt 12521_74_4.zip,0000786623.pdf 12521_15_5.zip,0000793892.pdf Desired Output ... (5 Replies)
Discussion started by: techmoris
5 Replies

7. UNIX for Dummies Questions & Answers

Adding EMPTY columns to Tab-delimited txt file

Hi I have a txt file with 4 columns where I need to add 4 empty columns in the middle meaning that I need what is currently column 4 to be column 8 in a new file. The idea is that I have to use the file as input in a program that reads the data in column 1 and 8, so the content of the other... (8 Replies)
Discussion started by: Banni
8 Replies

8. Shell Programming and Scripting

How to get the first column from the txt file using unix command?

Hi All, I have the file like this (file name is : tem_text) no Id name ccy ------- ---- ------------------- -------- 7777 17 India Overseas Partners 500INR I want to retreive the third colimn of from the above text file if i use the basic awk command cat... (4 Replies)
Discussion started by: psiva_arul
4 Replies

9. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

10. Shell Programming and Scripting

Creating/ammending Name Column in existing .txt file

With the help of this forum, I have a script with the following output: chr7 27104483 27105154 chr7 27106872 27110789 chr7 27111956 27112830 chr7 27114388 27125180 chr7 27126966 27131260 chr7 27135440 27137796 which was created by the following script: awk '1 == NR || $NF >= 1000 {... (6 Replies)
Discussion started by: awknerd
6 Replies
Login or Register to Ask a Question