sed to extract first two uppercase chars in targeted lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to extract first two uppercase chars in targeted lines
# 1  
Old 06-09-2008
sed to extract first two uppercase chars in targeted lines

Hello,

I have a file temp.txt:
-------------------------
HELLO WORLD
This is a temp file.
TENCHARSHEre
no beginning UPPERCHARS
HI There
-------------------------

What is a sed egrep command that will target lines that begin with 3-10 uppercase chars, and output the first 2 chars?
desired result:
---
HE
TE
---

So far I have been able to extract the targeted lines in egrep using the expression: egrep ^[A-Z]\{3,10\} < temp.txt . . but no luck in sed. Smilie

Thank you
# 2  
Old 06-09-2008
Code:
sed -n 's/^\([A-Z][A-Z]\)[A-Z].*/\1/p' temp.txt

# 3  
Old 06-09-2008
Thank you for the reply..
However I have to do the following using egrep.

I tried translating what you gave using -r:
sed -r 's/^([A-Z][A-Z])[A-Z].*/\1/g' temp.txt

Updated temp.txt:
-------------------------
HELLO WORLD
This is a temp file.
TENCHARSHEre
no beginning UPPERCHARS
HI There
ELEVENCHARActers
-------------------------
desired output:
HE
TE

actual output:
HE
This is a temp file.
TE
no beginning UPPERCHARS
HI There
EL

1) How do I ignore irrelevant targets? (i.e. "This is a temp file.", etc..)
2) It also includes 11 upperchars as the target. How do I restrict it to 10 uppercase chars only?

Sed and regex are very difficult concepts for me to grasp, so I appreciate the patience and time. Thank you
# 4  
Old 06-09-2008
Code:
sed -nr '/^[A-Z]{11}/!{s/^([A-Z]{2})[A-Z].*/\1/p;}' temp.txt

Or:

Code:
sed -nr 's/^([A-Z]{2})[A-Z]{1,8}[^A-Z].*/\1/p' temp.txt


Last edited by radoulov; 06-09-2008 at 04:30 PM..
# 5  
Old 06-09-2008
Thank you radoulov

The second method is a little more intutitive . . . Is there a way to execute the expression without the -n option?

Thanks again
# 6  
Old 06-10-2008
Quote:
Originally Posted by str8danked
Thank you radoulov

The second method is a little more intutitive . . . Is there a way to execute the expression without the -n option?
Why you need to remove the -n option?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

XML - Split And Extract String between Chars

Hi, I am trying to read the records from file and split into multiple files. SourceFile.txt <?xml version="1.0" encoding="UTF-8"?>... (2 Replies)
Discussion started by: unme
2 Replies

3. Shell Programming and Scripting

SED (or other) upper to lowercase, with first letter of first word in each sentence uppercase

The title pretty much defines the problem. I have text files that are all in caps. I would like to convert them to lowercase, but have the first letter of the first word in each sentence in uppercase. I already have SED on the server for fixing / tweaking text files, but I'm open to other... (5 Replies)
Discussion started by: dockline
5 Replies

4. Shell Programming and Scripting

making the first character of word using uppercase using awk and sed

I want to make the first character of some words to be uppercase. I have a file like the one below. uid,givenname,sn,cn,mail,telephonenumber mattj,matt,johnson,matt johnson,mattj@gmail.com markv,mark,vennet,matt s vennet,markv@gmail.com mikea,mike,austi,mike austin,mike@gmail.com I want... (3 Replies)
Discussion started by: matt12
3 Replies

5. Shell Programming and Scripting

sed discard chars after last _

Hi, I'd get fields like unix_linux_form_yyyyddmmhhmi.file.txt shell_programming_and_scripting.txt so on... and want them as below unix_linux_form shell_programming_and I could remove everything after a '.' as below echo $field | sed 's/\..*//' but how to remove... (14 Replies)
Discussion started by: dips_ag
14 Replies

6. Shell Programming and Scripting

Special chars in sed variable

Hi, For years ive been using this script to do mass search & replaces on our websites. Its worked with all sorts of spaces, quotes, html or whatever with a little adjusting here and there. But I just cant get this pattern to work: #!/bin/bash OLDURL="document.write('<script... (2 Replies)
Discussion started by: mutex
2 Replies

7. Shell Programming and Scripting

sed help to convert from lowercase to uppercase and vice versa!

Hello, can sed be used to convert all letters of a file from uppercase to lowercase and vice versa?i know tr command can be used but with sed is it possible? i came up with this :- sed 'y///' file1 actually the above command is also not working! Please help me. Thanks in advance :) (6 Replies)
Discussion started by: salman4u
6 Replies

8. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

9. Shell Programming and Scripting

Replace Junk chars (Sed)

I know this has been asked previously on this forum...But I think I have a different scenario to present. I ahve a file tht looks like this (note:there are control Z and other chars tht are not visible on line with anme bowers) BB7118450 6004718 BIANCALANA =HEI BZ5842819 ... (4 Replies)
Discussion started by: alfredo123
4 Replies

10. UNIX for Dummies Questions & Answers

Using SED to get n chars after given value from file

Hello, my name is Marc, I'm a linux starter :) and I hope you specialists can help me solving this issue. I have a file containing a lot of data. Somewhere in this file, there's a string called "Faultdump", directly followed by 64 chars of HEX data. I need to get the HEX part. I accomplished... (12 Replies)
Discussion started by: Kally
12 Replies
Login or Register to Ask a Question