Print character after pattern found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print character after pattern found
# 15  
Old 07-29-2013
Hi Don, Thanks for the response.
Basically print substr($i, 1, 10) will display a timestamp e.g. 2013-16-07.
I want this timestamp to be assigned in a variable e.g. "x".
Then ill add 5 days using the below command:
Code:
date -d "$x 5 days" +%F


Last edited by Scott; 07-29-2013 at 03:01 AM.. Reason: Code tags
# 16  
Old 07-29-2013
So, the third example I gave you is exactly what you want:
Code:
#!/bin/ksh
awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file |
while read d
do      date -d "$d + 5 days" +%F
done

I don't have a GNU date utility to play with and the Linux date man page is vague, but I think you need to add the plus sign that I marked in red above to get the dates 5 days after the given birthdays.
# 17  
Old 07-29-2013
Quote:
Originally Posted by Don Cragun
So, the third example I gave you is exactly what you want:
Code:
#!/bin/ksh
awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file |
while read d
do      date -d "$d + 5 days" +%F
done

I don't have a GNU date utility to play with and the Linux date man page is vague, but I think you need to add the plus sign that I marked in red above to get the dates 5 days after the given birthdays.

Thanks Don,
"date -d "$d 5 days" +%F" works so far in ubuntu. But i have another problem when executing the below commands:
Code:
while read d
do      
x=$(date -d "$d 5 days" +%F)
echo $x
sed 's/$d/$x/g' file > file2
done

I added "echo $x" just to see if its returning the correct output and yes, its displaying the correct one. But
the file2 content doesnt get updated with the correct date which is timestamp+5 days.

Last edited by Don Cragun; 07-29-2013 at 02:14 AM.. Reason: Add CODE tags
# 18  
Old 07-29-2013
You need to use double quotes if you want the shell to expand variables within a string.

And, please, to preserve formatting, start using code tags for code and data. Moderators have already edited multiple posts of yours in this thread for that reason.

Regards,
Alister
# 19  
Old 07-29-2013
Quote:
Originally Posted by alister
You need to use double quotes if you want the shell to expand variables within a string.

And, please, to preserve formatting, start using code tags for code and data. Moderators have already edited multiple posts of yours in this thread for that reason.

Regards,
Alister
Hi Alister,
Sorry. Ill take note of that code tags.
Thanks for the help Don. I got it resolved.

Last edited by scripter123; 07-29-2013 at 02:57 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

2. Shell Programming and Scripting

Copy/print all lines between pattern is found in .log files

Hi, I have a folder with multiple (< 33) .log files. And I have to copy the lines between two patterns from all the .log files to a new file. (script file with a loop?) Thanks in advance. 1.log ... .. xx1> begin ... .. .. >>> Total: 2 Alarms .. .. (17 Replies)
Discussion started by: AK47
17 Replies

3. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

4. Shell Programming and Scripting

Print Specific lines when found specific character

Hello all, I have thousand file input like this: file1: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ | | | |$$ $$ UERT | TTYH | TAFE | FRFG |$$ $$______|______|________|______|$$ $$ | | | |$$ $$ 1 | DISK | TR1311 | 1 |$$ $$ 1 |... (4 Replies)
Discussion started by: attila
4 Replies

5. Shell Programming and Scripting

Print characters till the next space when the pattern is found

i have a file which contains alphanumeric data in every line. what i need is the data after certain pattern. the data after the pattern is not of fixed length so i need the data till the space after the pattern. Input file: bfdkasfbdfg khffkf lkdhfhdf pattern (datarequired data not required)... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

6. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

7. Shell Programming and Scripting

Print the 2nd line everytime after defined pattern is found.

Hi, I have a text file similar to the example below and I want to print the second line every time after the "--------------------------" pattern is found. The pattern is a fixed length of - characters. Example of input; 1 -------------------------- 2 3 39184018234 4 ... (10 Replies)
Discussion started by: lewk
10 Replies

8. Shell Programming and Scripting

Find a pattern and print next all character to next space

Hi, I have a big inventory file that is NOT sorted is any way. The file is have "tagged" information like the ip address "*IP=" or the name "*NM=" . How do I get just the ip address or the name and not the whole line? I have tried to use AWK without any success. I always get the whole line... (8 Replies)
Discussion started by: pierrebjarnfelt
8 Replies

9. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question