Maintain line format using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Maintain line format using awk
# 1  
Old 09-09-2014
Maintain line format using awk

Hello
I have a file with the following format:
Code:
...
  text1      num   num   P
# 2014--2-28-22---6 33.76--38.4173---21.9403----0.08-0.00--0.01--0.01--0.46----------0
  text1      num   num   P
  text 2     num   num   S
  text 3     num   num   P
  ...

(where "-"=space, "spaces" cannot appear in the thread format)
What I need to do is change the last "0" with an increasing number + keeping the format of the line.

I have already created the script :
Code:
awk '/^#/ {$NF=++n}1' file

but it ruins the format of the line taking off the spaces. It looks like
Code:
# 2014 2 28 23 6 33.76 38.4173 21.9403 0.08 0.00 0.01 0.01 0.46 531

Thanks in advance for your help



Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 09-09-2014 at 12:56 PM..
# 2  
Old 09-09-2014
What's your system? If you have GNU awk you can use the FIELDWIDTHS variable to define columns instead of the field separator.

Code:
awk -v FIELDWIDTHS="2 6 2 3 4 2 7 10 10 5 6 6 6 15 10" '/^#/ { $NF=++N }' input > output

Adjust FIELDWIDTHS to taste, I may not have it exactly right if your data isn't precisely as shown.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-09-2014
Quote:
Originally Posted by Corona688
What's your system? If you have GNU awk you can use the FIELDWIDTHS variable to define columns instead of the field separator.

Code:
awk -v FIELDWIDTHS="2 6 2 3 4 2 7 10 10 5 6 6 6 15 10" '/^#/ { $NF=++N }' input > output

Adjust FIELDWIDTHS to taste, I may not have it exactly right if your data isn't precisely as shown.
thank you Corona688 for yr reply. I have GNU Awk 4.1.0, API: 1.0.
I tried your proposal but I don't get an output, probubly because the fields r not wright. are they like:
Code:
 #-2014--1--1--0-49-20.63--38.3330---22.0517----9.43-0.00--0.00--0.06--0.12----------0
     6 3  3  3  3     6        9         9       8    5     6     6     6         10
?

# 4  
Old 09-09-2014
I forgot the 1 on the end of the program, add it and try again.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-09-2014
Quote:
Originally Posted by Corona688
I forgot the 1 on the end of the program, add it and try again.
Thank you! It worked! the field widths got into my nerves though and I used:
awk -v FIELDWIDTHS=" 81 2 " '/^#/ { $NF=++N }1' but it also did the job!
Thanks again!
This User Gave Thanks to phaethon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

awk to format each line by pattern

The four lines in the tab-delimeted input are a sample format from my actual data. The awk is meant to go line by line and check if a pattern is satisfied and if it is follow a particular format (there are 3). All the lines in the file should follow one of the three formats below. I added comments... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Need to maintain in- and output format with awk

Hi All, I have a data file (myfile.txt) as below: - A H C - A HHH F - AAA HH I The importan point is that the width between the columns are not fixed and the column seperator is space. I wish to change the value of 4th column using awk only when $3 = HH. I can... (4 Replies)
Discussion started by: angshuman
4 Replies

4. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

5. Shell Programming and Scripting

awk maintain case query

# Print list of word frequencies { $0 = tolower($0) for (i = 1; i <= NF; i++) counter++ } END { for (word in counter) printf "%s\t%d\n",word, counter } I have this simple awk code from awk user guide to count the frequency of word. Now consider the... (7 Replies)
Discussion started by: ajacobs365
7 Replies

6. Shell Programming and Scripting

Log maintain in Perl

hi , While working on perl, have to write a continuous log file which I can monitor. I tried like, open (LOG,'>'.$log.'/Report_'.$todaysdate.'.log') or die("Unable to create the log file"); print LOG "\nDate : ".`date`; But when I ran the script in backend and monitor log, it... (1 Reply)
Discussion started by: Deei
1 Replies

7. UNIX for Dummies Questions & Answers

Just asking - Tips on how to maintain your scripts ..

Hi all, Just writing to ask if any one can advise on what tools to use best for maintaining your scripts ... preferably free/open source and portable if there is one, that is, one that can be placed and run on a USB stick ... At the moment, am having them in directories and files and no... (2 Replies)
Discussion started by: newbie_01
2 Replies

8. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 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

Maintain 30 days data

Hi Folks, I have a log file that keeps the information pn the date and time a specific transaction is firedup. I found that the log file keeps growing and I intend to limit the entry to the log file to 30 days. Log file name is transaction.log, here is the content: 120802_23:47:37 ... (3 Replies)
Discussion started by: odogbolu98
3 Replies
Login or Register to Ask a Question