Getting data with unstable space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting data with unstable space
# 1  
Old 10-03-2014
Getting data with unstable space

Please notice the date output. the Oct 3, has 2 spaces after Oct.
i want to have an awk or sed that will get the exact space, data. Because if you are GREPING a syslog file. Date and spaces are very important, else you cant get the right data

Code:
$  date
Fri Oct  3 06:03:35 MST 2014

Output:
date|awk '{print $2,$3}'
Oct 3


I can achieve this by: 
$ date|awk '{print $2,"",$3}'
Oct  3

But the problem is, when october reach double digit,like october 10-31, below command are not working anymore

Code:
$ date|awk '{print $2,"",$3}'

because EXAMPLE TODAY is October 10

Code:
$date
Fri Oct 10 06:03:35 MST 201

this command will work perfectly fine
date|awk '{print $2,$3}'

But what awk and sed. will get what ever kind of date.
# 2  
Old 10-03-2014
With printf you can use the width trick, something like:
Code:
date | awk '{ printf("%s %*s\n", $2, 2, $3) }'

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 10-03-2014
The logical way to get the abbreviated month and day is to use date by itself:
Code:
date '+%b %e'

If you insist on using awk:
Code:
date | awk '{printf("%s%3d\n", $2, $3)}'

If you insist on using sed:
Code:
date | sed 's/....\(......\).*/\1/'

And, if you like cut:
Code:
date | cut -c5-10


Last edited by Don Cragun; 10-03-2014 at 10:51 AM.. Reason: Quotes not needed on cut
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-03-2014
OR else something like this might help

Code:
 date | awk '{split($0,d,/[^[:space:]]*/); printf("%s%s%s\n",$2,d[3],$3)}'

I am bit confused , forgive me if I misunderstood your requirement.
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 10-03-2014
Quote:
Originally Posted by kenshinhimura
Please notice the date output. the Oct 3, has 2 spaces after Oct.
. . .
Please note that there's no "unstable spaces". The format is very predictable as the 2 spaces after Oct are
- the separator
- the space padded day-of-month output of date.
To avoid the space padding use another format string, e.g.
Code:
date +"%a %b %d %H:%M:%S %Z %Y"
Fri Oct 03 17:11:00 CEST 2014

as opposed to the default
Code:
date
Fri Oct  3 17:17:18 CEST 2014

This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-03-2014
unfortunately..i cant change the date...so i will adjust my command..

the system works likes this..

Code:
$date
Oct   3

if 10 days above
$date
Oct 11

But this works for me.. thanks guys

Code:
awk '{ printf("%s %*s\n", $2, 2, $3) }'

---------- Post updated at 02:21 PM ---------- Previous update was at 02:21 PM ----------

Quote:
Originally Posted by Franklin52
With printf you can use the width trick, something like:
Code:
date | awk '{ printf("%s %*s\n", $2, 2, $3) }'

awesome sir...

---------- Post updated at 02:23 PM ---------- Previous update was at 02:21 PM ----------

Quote:
Originally Posted by Don Cragun
The logical way to get the abbreviated month and day is to use date by itself:
Code:
date '+%b %e'

If you insist on using awk:
Code:
date | awk '{printf("%s%3d\n", $2, $3)}'

If you insist on using sed:
Code:
date | sed 's/....\(......\).*/\1/'

And, if you like cut:
Code:
date | cut -c5-10

this works as well..thanks sir...

Code:
date | awk '{printf("%s%3d\n", $2, $3)}

---------- Post updated at 02:32 PM ---------- Previous update was at 02:23 PM ----------

Don/Franklin

can you explain this? character by character?? Please???

thanks

Code:
awk '{printf("%s%3d\n", $2, $3)}'
 awk '{ printf("%s %*s\n", $2, 2, $3) }'

sed 's/....\(......\).*/\1/'

# 7  
Old 10-03-2014
Quote:
Originally Posted by kenshinhimura
... ... ...

Don/Franklin

can you explain this? character by character?? Please???

thanks

[CODE]awk '{printf("%s%3d\n", $2, $3)}'
awk '{ printf("%s %*s\n", $2, 2, $3) }'
Code:
sed 's/....\(......\).*/\1/'

Code:
awk '{printf("%s%3d\n", $2, $3)}'

  1. awk: utility to run.
  2. '{printf("%s%3d\n", $2, $3)}': script to be interpreted by awk.
  3. '{ commands }': run the specified commands for each line read from standard input.
  4. printf(format, arg1, arg2): print the specified arguments to standard output according to the formatting specifiers given in format.
  5. "%s": format the next argument ($2 in this case) as a string.
  6. "%3d": format the next argument ($3 in this case) as a 3 character string of decimal digits with leading space fill.
  7. "\n": print a literal <newline> character.
  8. ,: separate arguments in the parameter list of the function.
  9. $2: the 2nd field in the current input line. (By default, awk splits fields by sequences of one or more space and/or tab characters.)
  10. $3: the 3rd field in the current input line.
Code:
awk '{ printf("%s %*s\n", $2, 2, $3) }'

  1. awk: utility to run.
  2. '{printf("%s %*s\n", $2, $3)}': script to be interpreted by awk.
  3. '{ commands }': run the specified commands for each line read from standard input.
  4. printf(format, arg1, arg2): print the specified arguments to standard output according to the formatting specifiers given in format.
  5. "%s": format the next argument ($2 in this case) as a string.
  6. " ": print a literal space character.
  7. "%*s": format a string with width specified by the next argument (2 in this case) as right justified with leading space fill from the next argument ($3 in this case).
  8. "\n": print a literal <newline> character.
  9. ,: separate arguments in the parameter list of the function.
  10. $2: the 2nd field in the current input line.
  11. 2: the integer two.
  12. $3: the 3rd field in the current input line.
Code:
sed 's/....\(......\).*/\1/'

  1. sed: the utility to run.
  2. 's/....\(......\).*/\1/': the sed command to be executed.
  3. s/BRE/replacement/: for each line read from standard input, substitute the string specified by repacement for the string in the input line that matches the basic regular expression specified by BRE.
  4. ....: match any four contiguous characters.
  5. \(......\): match any six contiguous characters and remember them for use as a back reference.
  6. .* match the longest string of zero or more available characters. These three parts of the BRE when concatenated together match the entire input line and remember the 5th through the 10th characters on the line as a back reference.
  7. \1: replace the entire string matched by the BRE with the 1st substring remembered as a back reference.
  8. and print the modified input line.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert space or pattern between columns in a data file

I have a data file where three data sets are written in three columns. Can I increase the space between the columns without reading them? Also can I insert particular patterns, say comma between 1st and 2nd column and colon between 2nd and 3rd column? (13 Replies)
Discussion started by: hbar
13 Replies

2. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

3. Shell Programming and Scripting

Processing data that contains space and quote delimiters

I need to write a Bash script to process a data file that is in this format: 1 A B C D E 2 F G "H H" I J As you can see, the data is delimited by a space, but there are also some fields that contain spaces and are surrounded by double-quotes. An example of that is "H H". I wrote... (7 Replies)
Discussion started by: RickS
7 Replies

4. Shell Programming and Scripting

Export data from DB2 table to .txt file(space delimited)

Hi I need help on this. Its very urgent for me.. please try to help me out.. I have data in tables in DB2 database. I would like to export the data from DB2 tables into a text file, which has to be space delimited. so that I can carry out awk, grep operations on that file. I tried to export... (2 Replies)
Discussion started by: ss3944
2 Replies

5. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

6. Shell Programming and Scripting

Delete all data before first space occurence

Hi Guyz, Can anyone help me in the following:- I want to delete all that is there in my file that occures before the first space comes. Eg. My file InputFile conatins the following: 123 12345678 87654 Hello 09867 09876654 34567 Happy I want the data occuring before the occurence... (3 Replies)
Discussion started by: DTechBuddy
3 Replies

7. Programming

appending space in binary data in c

Hi ... I am having a string in the buffer .. It is binary data it may contain space . When i try to append a space in using strcpy it is taking the inbetween \n as the end and appending space .. How to append space in the binary data in C ?? please let me know Thanks in advance, Arun. (2 Replies)
Discussion started by: arunkumar_mca
2 Replies
Login or Register to Ask a Question