Extracting 22-character strings from text using sed/awk?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting 22-character strings from text using sed/awk?
# 1  
Old 09-15-2013
Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how.

I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make non-redunatant using uniq) of every 22-character string that ends in these specific 2 characters.

The input is a just flat file of just a long string of characters without line breaks (usually around 2000-10000 characters each):

The desired output would look like this:

Any help is appreciated!

Last edited by Twinklefingers; 09-15-2013 at 03:57 PM..
# 2  
Old 09-15-2013
Can you post sample input and desired output?
# 3  
Old 09-15-2013
This will work when
a) there's only one single occurrence of GG in a line
b) there's at least 20 char in front of the GG

sed -rn 's/^.*(.{20}GG).*$/\1/p' file

In case your sed doen't allow for EREs, try
sed -n 's/^.*\(.\{20\}GG\).*$/\1/p' file

# 4  
Old 09-15-2013
@bartus11 - Added in the post, thanks!

@RudiC - There are going to be more than one instances of GG in each line, however I can prepare the files for analysis by inserting a line break after each instances of GG.
# 5  
Old 09-15-2013
Sample input for that output would be nice to have as well.
# 6  
Old 09-15-2013
Some assumptions have to made here:-
1) The flatfile is pure alpha-numeric.
2) The "GG" ALWAYS has at least 20 characters before it that are NOT those two.
3) The file is not enormous.

In this example I have deliberately inserted GGs inside the 22 character bounderies to
show the possible error that could result.
#!/bin/bash --posix
echo "1234567890qwertyuiopasdfghjklzxcvbnmGGMNBVCXZLKJHGFDSAPOIUYTREWQ0987654321GG1234567890GGABCDEFGGHIJ" > /tmp/flatfile
read textfile < /tmp/flatfile
echo ""
echo "$textfile"
echo ""
for n in $( seq 0 1 ${#textfile} )
	if [ "${textfile:$n:2}" == "GG" ]
		then echo "${textfile:$[ ( $n - 20 ) ]:22}"
echo ""

And the display on OSX 10.7.5 default bash terminal:-
Last login: Sun Sep 15 18:17:17 on ttys000
AMIGA:barrywalker~> chmod 755 flatfile.sh
AMIGA:barrywalker~> ./flatfile.sh



AMIGA:barrywalker~> _

NOTE:- The "GG"s inside 22 characters also include earlier "GG"s!
# 7  
Old 09-15-2013
@bartus11 - Input added!

Last edited by Twinklefingers; 09-15-2013 at 04:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting strings at various positions of text file

Hi Team - I hope everyone has been well! I export a file from one of our source systems that gives me more information than I need. The way the file outputs, I need to extract certain strings at different positions on the file and echo them to another file. I can do this in batch easily,... (2 Replies)
Discussion started by: SIMMS7400
2 Replies

2. Shell Programming and Scripting

Extracting text between two strings, multiple instances

Hi experts, Ive got a text file which has the following text which will occur in this format at least one time: +=========================>> Some stuff that evreryone should knnow other stufsjdokajkajokajda aijhjajcdjajcisajcqsqdqwdqad <<=========================+ It is likely that... (8 Replies)
Discussion started by: martin0852
8 Replies

3. Shell Programming and Scripting

Extracting text between two constant strings

Hi All, I have a file whose common patter is like this: .I 1 .U 87049087 .S Some text here too .M This is a text .T Some another text here .P Name of the book .W Some lines of more text. This text needs to be extracted. .A more text goes here too .I 2 (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

4. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

5. Shell Programming and Scripting

Extracting text between two strings

Hi, I've looked at a few existing posts on this, but they don't seem to work for my inputs. I have a text file where I want to extract all the text between two strings, every time that occurs. Eg my input file is Anna said that she would fetch the bucket. Anna and Ben moved the bucket.... (9 Replies)
Discussion started by: JamesForeman
9 Replies

6. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

7. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

8. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

9. Shell Programming and Scripting

Extracting text between two strings, first instance only

There are a lot of ways to extract text from between two strings, but what if those strings occur multiple times and you only want the text from the first two strings? I can't seem to find anything to work here. I'm using sed to process the text after it's extracted, so I prefer a sed answer, but... (4 Replies)
Discussion started by: fubaya
4 Replies

10. Shell Programming and Scripting

extracting a set of strings from a text file

i have textfiles that contain a series of lines that look like this: string0 .................................................... column3a column4a string1**384y0439 ..................................... column3b column4b... (2 Replies)
Discussion started by: Deanne
2 Replies
Login or Register to Ask a Question