line break if string exceeds 100chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting line break if string exceeds 100chars
# 1  
Old 01-26-2011
line break if string exceeds 100chars

I am required to develop a script to look for specific strings in the /var/adm/messages file on our Solaris10 host. When an entry is found in the file then I need that line to be piped to a log file. I have a script with the criteria i just need help with the manipulation of the text string.

1. I need to replace the time stamps in /var/adm/messages and replace it with the format shown below. I need a way to replace collumn 1, 2 and 3 with the date example below.

date example:
Code:
# date '+%D %T'
01/26/11 12:40:56

2. I also need to make the string continue on a new line if it exceeds 100 chars, so that the log file looks pretty and not wrapped around on a standard sized monitor.

original line:
Code:
Jan  26 12:40:56 hostname usba: [ID 912658 kern.info] USB 2.0 device (usb430,a2) operating at full speed (USB 1.x) on USB 2.0 external hub: keyboard@4, hid3 at bus address 5

format I would like in my log file:
Code:
01/26/11 12:40:56 hostname usba: [ID 912658 kern.info] USB 2.0 device (usb430,a2) operating at full
01/26/11 12:40:56 speed (USB 1.x) on USB 2.0 external hub: keyboard@4, hid3 at bus address 5

# 2  
Old 01-26-2011
Try this,

Code:
awk -v yr=`date '+%Y'` 'BEGIN {
m["Jan"]="01"
m["Feb"]="02"
m["Mar"]="03"
m["Apr"]="04"
m["May"]="05"
m["Jun"]="06"
m["Jul"]="07"
m["Aug"]="08"
m["Sep"]="09"
m["Oct"]="10"
m["Nov"]="11"
m["Dec"]="12"
} 
{dt=m[$1]"/"$2"/"yr FS $3;printf dt FS; l=18;for(i=4;i<=NF;i++){l+=length($i)+1;if(l>100 && flg == 1) {flg=0;l=18;printf RS dt FS $i FS} else {flg=1;printf $i FS }}printf "\n"}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 01-26-2011
Thanks, I gave it a try but I'm doing something wrong.
I am really rubbish at awk so I need some help here. Must I feed this awk command line by line of the /var/adm/messages or can I just specify the filename at the end by replacing 'inputfile'? I tried that and I got this very non-descriptive return:
Code:
awk: syntax error near line 1
awk: bailing out near line 1

# 4  
Old 01-26-2011
For Solaris use /usr/bin/nawk or /usr/xpg4/bin/awk
# 5  
Old 01-27-2011
cool it works, thanks
may I maybe ask you to explain the logic behind the command to me? I have run through awk docs, but this still seems a little over my head.
# 6  
Old 01-27-2011
Code:
awk 
-v yr=`date '+%Y'` # Sets the variable yr to the value current year before execution of the program begins
'BEGIN {
m["Jan"]="01"
m["Feb"]="02"
m["Mar"]="03"
m["Apr"]="04"
m["May"]="05"
m["Jun"]="06"
m["Jul"]="07"
m["Aug"]="08"
m["Sep"]="09"
m["Oct"]="10"
m["Nov"]="11"
m["Dec"]="12"
}              #the BEGIN block is evaluated before awk starts processing the input file, We initialize array "m" with index as month and value as month's number 
{	dt=m[$1]"/"$2"/"yr FS $3; # here $1 = Jan,$2= 26 and $3=12:40:56 as per your input file, store date as per your formate into varibale dt. 
	printf dt FS;  # print date and FS (filed seperator)
	l=18; # here l=18 , length of date and time which we alerady printed.
	for(i=4;i<=NF;i++) # start for loop from field 4
	{
		l+=length($i)+1; # adding length of field and 1 for field seperator
		if(l>100 && flg == 1) # if this condition is true means we already print 100 char.
		{flg=0;l=18;printf RS dt FS $i FS} # reset the flag and length variable and print date time and field on new line 
	else {flg=1;printf $i FS } # condition is false then print field on same line
	} 
printf "\n"}' inputfile

# 7  
Old 01-27-2011
cool thanks a stack man
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find a string across line break (because of "segmentation fault core dumped")

Hi, thanks to a precedent post, and thanks to the reply of derekludwig of the forum, I have convert my first awk command as : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL awk -f findstring.awk test.txt > textreturn.txtfindstring.awk is : BEGIN{ SLENGTH = 3 } { ... (3 Replies)
Discussion started by: thewizarde6
3 Replies

2. Shell Programming and Scripting

How to break the line to the one above?

Hello everyone! I'm trying to make the below file1 look like file2, can anyone help? Basically I just hit backspace on every line that starts with a number. Thanks! file1: THIS#IS-IT1 4 THIS#IS-IT2 3 THIS#IS-IT3 2 THIS#IS-IT4 1 Result > file2: (4 Replies)
Discussion started by: demmel
4 Replies

3. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

4. UNIX for Dummies Questions & Answers

Insert a break page after certain string using SED

Hi: I have 2 files: teststring.txt and a tempfile.txt teststring file contains: s/Primary Ins./\n1/g I'm trying to search for "Primary Ins." string in tempfile. For every "Primary Ins." string that is found, a new line is inserted and put in number 1. Then, write out the newfile... (7 Replies)
Discussion started by: newbeee
7 Replies

5. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

8. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

9. Shell Programming and Scripting

Break down a string

I am wondering how can I take a variable that may have multiple items of data and to use each one indenpendently. For example lets say that.... data=/lcl/apps/trm, /lcl/apps/wwe, /prd/sse/qwe, /lcl/ppe/eer Now I would like to be able to process each item found within the data string. As... (5 Replies)
Discussion started by: LRoberts
5 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question