Print character after pattern found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print character after pattern found
# 8  
Old 07-25-2013
Quote:
Originally Posted by rveri
scripter123,

Check this out:
Code:
awk '{A=split($0,a,/"/);for(i=1;i<=A;i++) if(a[i]~/birthday/) print a[i+1]}' file
1977-16-07
1975-16-07
1970-16-07

Enjoy ,Have fun!.


Thanks.
I think i would need to give the exact sample of the content.

See below:
Code:
my birthday="1977-15-07L5093" my birthday="1967-15-07L5678"
my birthday="1970-15-07L5093"

My desired output is the Date e.g. 1977-15-07 which is the next 10 characters after (birthday=").
For the above sample line, i should have the below output:
Code:
1977-15-07
1967-15-07
1970-15-07

---------- Post updated at 03:29 AM ---------- Previous update was at 12:54 AM ----------

Quote:
Originally Posted by Don Cragun
In your sample, the pattern doesn't matter, and your statement specifying your requirements doesn't match the output you say you want (the double quotes are missing in the output).

To get the output you requested from the input you gave (if I guessed correctly about where the CODE tags should have been), you could try something simple like:
Code:
awk -F'"' '{for(i = 2; i < NF; i += 2) print $i}' file

If file contains your sample input, the output produced matches your desired output.

As always, if you're using a Solaris/SunOS system, you need to use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /bin/awk or /usr/bin/awk.
Hi Don, Sorry for the confusion:
I think i would need to give you the exact sample of the file content.

See below:
Code:
my birthday="1977-15-07L5093" my birthday="1967-15-07L5678"
my birthday="1970-15-07L5093"

My desired output is the Date e.g. 1977-15-07 which is the next 10 characters after (birthday=").
For the above sample line, i should have the below output:
Code:
1977-15-07
1967-15-07
1970-15-07

Appreciate your help on this. Thanks

Last edited by Scott; 07-25-2013 at 06:24 AM.. Reason: Added code tags
# 9  
Old 07-25-2013
With your new requirements and new sample input, there is still no need to look for birthday=". The following simple awk script produces your new required output with your new exact sample:
Code:
awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file

What we really need is not an exact sample, but an exact specification of what is allowed in the input this program is supposed to process. But, giving us samples that are not representative of your input just wastes your time and ours.

Last edited by Don Cragun; 07-26-2013 at 12:20 AM.. Reason: Fix typo
# 10  
Old 07-25-2013
>> But, giving us samples that are not representative of your input just wastes your time and ours.
- I Totaly agree with Don. Scripter123 please note it down before posting next time, to have correct sample in first place.
# 11  
Old 07-25-2013
apologies gentlemen.
# 12  
Old 07-26-2013
And I guess that the real life data is more than 2 lines.
Also in post #1 its used , to separat fileds, this is gone in next example
# 13  
Old 07-26-2013
Quote:
Originally Posted by Don Cragun
With your new requirements and new sample input, there is still no need to look for birthday=". The following simple awk script produces your new required output with your new exact sample:
Code:
awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file

What we really need is not an exact sample, but an exact specification of what is allowed in the input this program is supposed to process. But, giving us samples that are not representative of your input just wastes your time and ours.

Hi Don, Thanks, the command works well and apologies on the previous confusion.

I have a follow query on how would i put into a variable the return dates.
print substr($i, 1, 10) - as what i understand, this line will print the return date, how would i put it in a variable?
# 14  
Old 07-26-2013
Quote:
Originally Posted by scripter123
Hi Don, Thanks, the command works well and apologies on the previous confusion.

I have a follow query on how would i put into a variable the return dates.
print substr($i, 1, 10) - as what i understand, this line will print the return date, how would i put it in a variable?
Yes, the awk command:
Code:
print substr($i, 1, 10)

prints the up to 10 characters starting with the 1st character from field number i in the current input line.

What kind of variable? What do you want to do with this variable? You're being vague about what you want again!

Is this what you mean?
Code:
awk -F'"' '
{	for(i = 2; i < NF; i += 2) {
		d = substr($i, 1, 10)
		print d
	}
}' file

Is this what you mean?
Code:
awk -F'"' '
{	for(i = 2; i < NF; i += 2)
		d[++dc] = substr($i, 1, 10)
}
END {   for(i = 1; i <= dc; i++)
		print d[i]
}' file

Both of the above shell scripts produce the same output as my previous awk command, but run slower. For "large" input files, the second one also takes more memory and for "HUGE" input files will eventually run out of memory.

Is this what you mean?
Code:
#!/bin/ksh
awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file |
while read d
do      echo $d
done

The above shell script will work with any POSIX conforming shell.

Is this what you mean?
Code:
#!/bin/ksh
d="$(awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file)"
echo $d | {
        read -A da
        i=0
        while [ $i -lt ${#da[@]} ]
        do      echo ${da[$i]}
                i=$((i + 1))
        done
}

The above shell script only works with 1993 and later versions of the Korn shell.

Is this what you mean?
Code:
#!/bin/bash
d="$(awk -F'"' '{for(i = 2; i < NF; i += 2) print substr($i, 1, 10)}' file)"
echo $d | {
        read -a da
        i=0
        while [ $i -lt ${#da[@]} ]
        do      echo ${da[$i]}
                i=$((i + 1))
        done
}

The above shell script only works with bash.

The last three shell scripts above all also produce the same output as my last awk command, but are slower yet. With sufficiently large input files, the last two could run into LINE_MAX, ARG_MAX, shell array size, and/or amount of memory available to the shell limitations.

Obviously there are thousands of other ways to assign output from a utility into variables into variables of various types in various programming languages. If you don't give us details about what you want, we can waste lots of time trying to guess what you want; or we can just ignore future requests for help from you.

Many people post vague questions like yours and the often get results. A few people post questions like:
I am using the WWW shell on XXX hardware running the YYY version of the ZZZ operating system. i have input files with the following input format: description of format
and here is a representative sample of an input file in this format:

input file name:
Code:
sample input

I want to produce output with the following characteristics: description of output
and here is the output I want to have produced from the input sample above:

output file name:
Code:
exact output desired for the given sample input

Here is what I have tried so far:
Code:
sample code

but it gives me the following errors or fails to produce the output that I want for reasons that I don't understand:
Code:
exact output from the above code when processing the sample input above

Can you help me correct my code, explain what I'm doing wrong, or tell me where to find an answer for my problem?

People who take the time and effort to start a thread with a post like that almost always get a quick, accurate, and working solution VERY quickly. And future posts from these people are naturally given a much higher priority by the volunteers in The UNIX and Linux Forum who try to help people asking questions.
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