Visit Our UNIX and Linux User Community


awk - How to preserve whitespace?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - How to preserve whitespace?
# 1  
Old 02-06-2013
Question awk - How to preserve whitespace?

Given a file:

Code:
# configuration file for newsyslog
# $FreeBSD: /repoman/r/ncvs/src/etc/newsyslog.conf,v 1.50 2005/03/02 00:40:55 brooks Exp $
#
# Entries which do not specify the '/pid_file' field will cause the
# syslogd process to be signalled when that log file is rotated.  This
# action is only appropriate for log files which are written to by the
# syslogd process (ie, files listed in /etc/syslog.conf).  If there
# is no process which needs to be signalled when a given log file is
# rotated, then the entry for that file should include the 'N' flag.
#
# The 'flags' field is one or more of the letters: BCGJNUWZ or a '-'.
#
# Note: some sites will want to select more restrictive protections than the
# defaults.  In particular, it may be desirable to switch many of the 644
# entries to 640 or 600.  For example, some sites will consider the
# contents of maillog, messages, and lpd-errs to be confidential.  In the
# future, these defaults may change to more conservative ones.
#
# logfilename          [owner:group]    mode count size when  flags [/pid_file] [sig_num]
/var/log/appfirewall.log		640  5     1000	*     J
/var/log/ftp.log			640  5	   1000	*     J
/var/log/hwmond.log			640  5	   1000	*     J
/var/log/install.log			640  5	   1000	*     J
/var/log/ipfw.log			640  5	   1000	*     J
/var/log/lookupd.log			640  5	   1000	*     J
/var/log/lpr.log			640  5	   1000	*     J
/var/log/mail.log			640  5	   1000	*     J
/var/log/ppp.log			640  5	   1000	*     J
/var/log/secure.log			640  5	   1000	*     J
/var/log/system.log			640  7	   *	@T00  J
/var/log/wtmp				644  3	   *	@01T05 B

I want to change the third, fourth, and fifth fields, so I wrote:

Code:
awk '/^\// {$3=13}{$4="*"}{$5="$W1"}{print}' newsyslog.conf

However, that results in:

Code:
# configuration file * $W1
# $FreeBSD: /repoman/r/ncvs/src/etc/newsyslog.conf,v * $W1 00:40:55 brooks Exp $
#   * $W1
# Entries which * $W1 specify the '/pid_file' field will cause the
# syslogd process * $W1 signalled when that log file is rotated. This
# action is * $W1 for log files which are written to by the
# syslogd process * $W1 listed in /etc/syslog.conf). If there
# is no * $W1 needs to be signalled when a given log file is
# rotated, then * $W1 for that file should include the 'N' flag.
#   * $W1
# The 'flags' * $W1 one or more of the letters: BCGJNUWZ or a '-'.
#   * $W1
# Note: some * $W1 want to select more restrictive protections than the
# defaults. In * $W1 may be desirable to switch many of the 644
# entries to * $W1 600. For example, some sites will consider the
# contents of * $W1 and lpd-errs to be confidential. In the
# future, these * $W1 change to more conservative ones.
#   * $W1
# logfilename [owner:group] * $W1 size when flags [/pid_file] [sig_num]
/var/log/appfirewall.log 640 13 * $W1 J
/var/log/ftp.log 640 13 * $W1 J
/var/log/hwmond.log 640 13 * $W1 J
/var/log/install.log 640 13 * $W1 J
/var/log/ipfw.log 640 13 * $W1 J
/var/log/lookupd.log 640 13 * $W1 J
/var/log/lpr.log 640 13 * $W1 J
/var/log/mail.log 640 13 * $W1 J
/var/log/ppp.log 640 13 * $W1 J
/var/log/secure.log 640 13 * $W1 J
/var/log/system.log 640 13 * $W1 J
/var/log/wtmp 644 13 * $W1 B

And I just realized it was acting on every line, not lines starting with a '/'

So, I now have two questions... one is, how do I preserve the original whitespace; two, how to I get multiple substitutions limited to those lines that match my pattern?
# 2  
Old 02-06-2013
How about:

Code:
awk '/^[/]/ {
    L=$0
    s=index(L, $3)
    L=substr(L,0,s-1) "13" substr(L,s+length($3));
    s=index(L, $4)
    L=substr(L,0,s-1) "*" substr(L,s+length($4));
    s=index(L, $5)
    $0=substr(L,0,s-1) "$W1" substr(L,s+length($5));
} 1' newsyslog.conf


Last edited by Chubler_XL; 02-06-2013 at 07:36 PM..
# 3  
Old 02-06-2013
Quote:
Originally Posted by Chubler_XL
How about:

Edit: Withdrawn "*" causing issues with sub
Code:
awk: nonterminated character class ^[
 source line number 1
 context is
	 >>> /^[/ <<<

Didn't like that either... :-)

Removing the brackets and escaping the forward slash got me to where you wound up.
# 4  
Old 02-06-2013
With GNU awk >= 4 you could try something like this:

Code:
awk '/^\// {
  n = split($0, t, FS, s)
  t[3] = 13
  t[4] = "*"
  t[5] = "$W1"
  for (i = 0; ++i <= n;)
    printf "%s", t[i] s[i]
  print x
  next
  }1' infile

# 5  
Old 02-06-2013
try also:
Code:
awk '
BEGIN {c[3]=13; c[4]="*"; c[5]="$W1"}
$0 ~ /^\// {
   l=$0; o="";
   for (i=1; i<=NF; i++) {
     w=l; sub("[ \t].*", "", w);
     o=o (c[i] ? c[i] : w);
     sub("^[ \t]*", "", l);
     sub("^[^ \t]*" , "", l);
     w=l; sub("[^ \t].*", "", w);
     o=o w; sub("^[ \t]*", "", l);
   }
   $0=o;
} 1
' newsyslog.conf

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 02-06-2013
Quote:
Originally Posted by radoulov
With GNU awk >= 4 you could try something like this:
I'm on a Mac, and have to use the BSD tools that come with OSX, as I could not count on GNU tools being available :-(
# 7  
Old 02-06-2013
Still unhappy with my solution, think this works much better:

Code:
awk '
function rep(a,v) {
  sub("^(([^ \t]+[ \t]+){"a-1"})","&" SUBSEP);
  sub(SUBSEP "[^ \t]+", v);
}   
/^\// {
    rep(3,"13")
    rep(4,"*")
    rep(5,"$W1")
} 1' newsyslog.conf


Last edited by Chubler_XL; 02-06-2013 at 08:49 PM..

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

awk to ignore whitespace in field

The awk below executes and update the desired field in my first awk. However, the white space between nonsynonymous SNV in $9 is being split into tabs and my attempt to correct this does not update the field unless it is removed. I am not sure what I am doing wrong? Thank you :). file1 ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Awk: Dealing with whitespace in associative array indicies

Is there a reliable way to deal with whitespace in array indicies? I am trying to annotate fails in a database using a table of known fails. In a begin block I have code like this: # Read in Known Fail List getline < "'"$failListFile"'"; getline < "'"$failListFile"'"; getline <... (6 Replies)
Discussion started by: Michael Stora
6 Replies

4. Shell Programming and Scripting

How to preserve spaces in input fields with awk?

I'm trying to do something pretty simple but its appears more complicated than expected... I've lines in a text file, separated by the comma and that I want to output to another file, without the first field. Input file: file1,item, 12345678 file2,item, 12345678 file2,item, ... (8 Replies)
Discussion started by: Armoric
8 Replies

5. UNIX for Dummies Questions & Answers

[Solved] How remove leading whitespace from xml (sed /awk?)

Hi again I have an xml file and want to remove the leading white space as it causes me issues later in my script I see sed is possible but cant seem to get it to work I tried sed 's/^ *//' file.xml output <xn:VsDataContainer id="1U104799" modifier="update"> ... (10 Replies)
Discussion started by: aniquebmx
10 Replies

6. Shell Programming and Scripting

Preserve trailing whitespace in variable

Hello, I wondering how I can echo a string without having the trailing whitespace removed. For example I have a string str="TESTING123 " that I need to hash using sha1. I get the correct answer when I run the line below from the terminal $ echo -n "TESTING123 " | openssl sha1... (3 Replies)
Discussion started by: colinireland
3 Replies

7. Shell Programming and Scripting

Date Format - preserve whitespace

I am trying to change the date format for the following: YESTER=`TZ=aaa24 date +%b" "%d | sed 's/0/ /'` TraceList=$(ls -ltR /pdt/logs | grep "$YESTER" | awk '{print $9}') CMD2=$(find /disk/dan/dansFiles/pass/logs/$TList -name cmd\* -prune) what I am trying to do in the above... (1 Reply)
Discussion started by: ther2000
1 Replies

8. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

9. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

10. Shell Programming and Scripting

Unable to assign value to variable using awk coz of whitespace in value

Unix gurus, I have a file as below, which is basically the result set obtained from a sql query on an Oracle database. ID PROG_NAME USER_PROG_NAME -------- --------------- ---------------------------------------- 33045 INCOIN Import Items 42690 ... (3 Replies)
Discussion started by: sunpraveen
3 Replies

Featured Tech Videos