how to add extra a field in a flat txt file ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add extra a field in a flat txt file ?
# 1  
Old 04-08-2011
how to add extra a field in a flat txt file ?

Hi all,

I did not use UNIX for a long time, now i need to make a flat file with extra field, can you help me with the code ?
1. I create a last line of each log from each system and make it in a flat text file (seperate by a pipe |)
mv current.log old
tail -1 sanfrancisco.log > current.log
tail -1 oakland.log >> current.log

Current.log
Code:
2011/04/07 14:49:46 |   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/

2. How do we create new.log which have a hostname in a 2nd fields ?
Code:
2011/04/07 14:49:46 |sanfrancisco|   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |oakland|  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/

Thanks,
# 2  
Old 04-08-2011
Code:
sed "s/|/|${HOSTNAME}|/" current_log


Last edited by Yogesh Sawant; 04-09-2011 at 10:37 AM.. Reason: added code tags
# 3  
Old 04-08-2011
try:
Code:
awk '{$3=$3 FILENAME"|";sub(/.log/,"",$3);if(FNR<X)print l;X=FNR;l=$0}END{print l}' logfiles >current.log

# 4  
Old 04-08-2011
Thanks all.
I create another host.log
sanfrancisco|
oakland|

Then paste host.log current.log > newlog.txt
I will try your way.
But now how do i get a newlog.txt or current.log like
Code:
2011/04/07 14:49:46 |sanfrancisco|goniners|   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |oakland|gosteelers|  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/

# 5  
Old 04-08-2011
Code:
$ HOST="myhost"
$ ruby -pne '$_.sub!(/\|/,"|'$HOST'|")' file
2011/04/07 14:49:46 |myhost|   61:linux   pid=027546 opened Boards 0, 1, 2, 3 for /home/goniners/abcd/
2011/04/08 08:13:39 |myhost|  255:linux   pid=016870 opened Boards 0, 1, 2, 3, 4, 6 for /home/gosteelers/asdas/

# 6  
Old 04-13-2011
Thanks, i never use Ruby before :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 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. Shell Programming and Scripting

awk to place value at 24 field in a flat file issue

I am trying to add 0393 value at 24th feild using the below command, but its adding at all the lines including header and trailer Input file: ZHV|2657|D0217001|T|TXU|Z|PAN|20131112000552||||OPER| 754|52479| 492|489|SP40|1014570286334|20131111|20131201|14355334|CHAMELON... (1 Reply)
Discussion started by: Aditya_001
1 Replies

4. UNIX for Dummies Questions & Answers

To Add extra commas to a CSV file.

Hi All, I got this requirement to process a complex CSV file. Eg File. Line 1: Name:,XYz Line 2: Age:,15 Line 3: Grade:,7 Line 4: Line 5: English, Maths, Science,Spanish Line 6:10,11,13,14 As you can see the maximum column is 4 . The file i need to make is Line 1: Name:,XYz,,... (12 Replies)
Discussion started by: chillblue
12 Replies

5. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

6. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

8. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

9. Shell Programming and Scripting

`find`, pulling 1st field from ASCII flat file as search/-name?

Hey Everyone! I have searched around for this on Unix.com and Google, and I'm either not phrasing my search properly or this is not as simple as I thought... I have a script that runs on a nightly basis that pulls one field worth of data from an internal MySQL database and populates to an... (2 Replies)
Discussion started by: Gecko12332
2 Replies

10. Shell Programming and Scripting

Reindex or re-increment last field of txt file.

I am using a database text file with a field that increments +1 with each new entry, occasionally if a entry is deleted the unique sequence is disrupted. I am looking for a small script/function in sh and/or perl that would re index this. Example of db file: Name | Address | misc |number... (2 Replies)
Discussion started by: silenthands
2 Replies
Login or Register to Ask a Question