sed questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed questions
# 15  
Old 08-14-2007
Quote:
Originally Posted by matrixmadhan
Look at this output !
Code:
echo "21237890" | awk '{ print substr($0, 0, 4) }'
2123

echo "21237890" | awk '{ print substr($0, 1, 4) }'
2123

Though the index starts at 1, there isn't any problem if ' 0 ' is used as it would default to ' 1 '.

and for the date format.

OP had specified that the input file would contain date value of the form YYYYMMDD
then what is the need to explicitly specify the format specifier when we are sure that we are going to extract ' 4 ' digit - ' 2 ' digit - ' 2 ' digit
Smilie

I don't really find the need for that.
matrixmadhan,

With your solution I get an output as:

Code:
199-1-1,US,"2"
200-3-3,AU,"w"

instead of:

Code:
1990-01-01,US,"2"
2001-03-03,AU,"w"

and that's not in the form YYYY-MM-DD Smilie

Regards
# 16  
Old 08-14-2007
surprising !

I get the following output

Code:
cat  3
date|country|ticker
19990101|US|2
20010303|AU|w

Code:
awk -F"|" '{ if ( NR == 1 ) { print } else { printf "%d-%d-%d,%s,\"%s\"\n", substr($1, 0, 4), substr($1,5,2), substr($1,7,2), $2, $3 } }' 3
date|country|ticker
1999-1-1,US,"2"
2001-3-3,AU,"w"

But I accept that printf format specifier should be used as

printf "%04d-%02d-%02d
# 17  
Old 08-14-2007
matrixmadhan,

Maybe you're right but I can't explain it, with:

Code:
echo "19990101"|awk '{print substr($0, 0, 4)}'

I get as output:

Code:
199

It don't work properly if I start the substring with 0.
I'm working home with awk version 1.2 (Dec 22 1994) on a Linux box (Debian Etch), I'll try it tomorrow on a HP machine.

Thanks and best regards
# 18  
Old 08-14-2007
Code:
$ echo "19990101"|awk '{print substr($0, 0, 4)}'
1999
$ echo "19990101"|awk '{print substr($0, 1, 4)}'
1999

I guess some awk's are more 'permissive' than others - try it with '1'
# 19  
Old 08-14-2007
Quote:
Originally Posted by vgersh99
Code:
$ echo "19990101"|awk '{print substr($0, 0, 4)}'
1999
$ echo "19990101"|awk '{print substr($0, 1, 4)}'
1999

I guess some awk's are more 'permissive' than others - try it with '1'
Ok, I'm confinced and 1 works properly, though in that case, it's for reason of compatabilty better to start with 1, which is used in a lots of tutorials and books.

Regards
# 20  
Old 08-15-2007
hi, try this one

For AWK you can try follow:
Code:
sed 's/|/,/g' filename | awk 'BEGIN{FS=","}
{
if (NR==1)
print $0
else
{
printf("%s-%s-%s,%s,\"%s\"\n",substr($1,1,4),substr($1,5,2),substr($1,7,2),$2,$3)
}
}'

For SED you can try follow:
Code:
sed  -e 's/\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1-\2-\3/' 
       -e '2,$s/\([a-zA-Z0-9]\)$/"\1"/' 
      -e 's/|/,/g' 
filename


Last edited by summer_cherry; 08-15-2007 at 06:11 AM..
# 21  
Old 08-15-2007
Next time i might try nawk and use some of your (this means all of you) suggestions. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

2. Shell Programming and Scripting

Sed/grep questions

Hi. I have a txt file. I need to make a copy of the lines which are beginning with a mobile phone number, or a fix phone number. I have to copy thoose lines in numbers.txt, after that i have to delete then from the originally file. In numbers.txt i need to write a prefix before each number. if the... (1 Reply)
Discussion started by: T720
1 Replies

3. Shell Programming and Scripting

grep and sed exact match questions

This post was previously mistaken for homework, but is actually a small piece of what I working on at work. Please answer if you can. QUESTION1 How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (2 Replies)
Discussion started by: thibodc
2 Replies

4. UNIX for Dummies Questions & Answers

grep and sed exact match questions

This was mistaken as homework in a different forum, but is not. These are questions that are close to what I am trying to do at work. QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

6. Shell Programming and Scripting

...yet another string of awk/sed questions from a RegExp-Challenged luser %-\

Greetings all, ...here is yet another string of awk/sed questions from a RegExp-Challenged luser :eek: I'm looking to have sed/awk do some clean-up on routing tables and to that end, I would like to do the following: 1.) If a line contains the word "masks" or "subnets" prepend CR/LF to... (16 Replies)
Discussion started by: SteveB-in-LV
16 Replies

7. Programming

two questions

hey all, I have question when am writing simple shell... in the child am calling execvp, i want the parent to know when execvp returns - 1. how can i let the parent know the result of execvp thanks in advance (9 Replies)
Discussion started by: joey
9 Replies

8. UNIX for Dummies Questions & Answers

Sed questions-Please Help

I am using this command to replace a text string in a data file: sed -e 's/ab/test2/g' -e 's/abcxyz/test1/g' datafile datafile contains: ab abcxyz My results: test2 test2cxyz I want my results to be: test2 test1 (7 Replies)
Discussion started by: bobo
7 Replies

9. UNIX for Dummies Questions & Answers

Unpratical SED and GREP questions

Hello every one, I have read a little about SED and GREP but I do not know how to do this: Using SED or GREP: "reverse all three letter words" "replace the last two digits in any string of digits by zeros (0)" "remove lines that start and end with the same word" and I have more like... (5 Replies)
Discussion started by: Lem2003
5 Replies
Login or Register to Ask a Question