Matching a pattern between two characters (sed)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching a pattern between two characters (sed)
# 1  
Old 02-23-2011
Matching a pattern between two characters (sed)

Hi there,

I have a file with lines like these:
0105:ffff0000:001b:01f4:25:434
0299:ffff0000:0009:01f4:2:319
02d2:ffff0000:000e:01f4:2:507

The above values are split up using ":" characters.
I would like capture each value, no matter what length.

Take for example the first line 0105:ffff0000:001b:01f4:25:434.
First value: 0105 (should be captured from the start of the line until the first ":")
Second value: ffff0000 (everything between the first ":" and the second ":")
Third value: 001b (everything betweed the second ":" and the third ":")
and so on..
There is always 6 values split up between 5 ":"-characters.

I tried the following for the first value:
Code:
while read line
do
  first=`echo $line | sed "s/^\(....\).*/\1/"`
  echo $first
  #second=`echo $line | sed ??`
  #third=`echo $line | sed ??`
  #fourth=`echo $line | sed ??`
  #fifth=`echo $line | sed ??`
  #sixth=`echo $line | sed ??`
done<file.txt

This will print the first four characters from the line for the $first var.
The first value is always four characters long, but the rest of the values can differ in length.
That's why I need to do the above approach by only looking between the ":"-characters.
Is it possible by only using sed?

Thanks in advance
# 2  
Old 02-23-2011
Code:
first=$(awk 'BEGIN{OFS=FS=":"}$2{print $2}' file)
echo $first

you can modify the field accordingly as well as the echo to bring you one value at a time
# 3  
Old 02-23-2011
The script is going to run on a busybox-enviroment.
The awk application is not available, only sed and grep.
# 4  
Old 02-23-2011
i don't know why you are using sed command.

you mention that data is already exist.
then you can use awk command like this.
Code:
first_val=`cat filename|head -1|awk -F":" '{print $1}'`
sec_val=`cat filename|head -1|awk -F":" '{print $2}'`
thrd_val=`cat filename|head -1|awk -F":" '{print $3}'`

if you want for a file then use head $variable in loop.
if you have any doubt please send a mail.

regards
rajesh

Last edited by Franklin52; 02-24-2011 at 03:27 AM.. Reason: Please use code tags, thank you
# 5  
Old 02-23-2011
I'm afraid the environment doesn't ship with "awk".
So there is no way to do the same thing with only sed?
If not then I'll have to ask the software-maintainer to recompile a new build for our platform including awk.
# 6  
Old 02-23-2011
Code:
$ while read line
> do
> echo "$line" | sed 's/:/ /g' | while read a b c d e f
> do
> echo $a
> echo $b
> echo $c
> echo $d
> echo $e
> echo $f
> done
> done <  input_file

# 7  
Old 02-23-2011
Code:
while IFS=: read a b c d e f junk
do
    echo "a->[${a}]"
    .....
done < myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sed to get first matching pattern

HI, I have a file: listenerport: - 1521 - 10520 - 10521 - 10522 - 10523 instances: listenerport: listenerport: - 1521 - 10540 - 10541 - 10542 - 10543 instances: ... (5 Replies)
Discussion started by: netbanker
5 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

Matching a pattern 250 characters up and down stream

hii all i have a file a which contains some thing like this strand smthg position + yyx 3020 - yyw 10,000 now i have another file (file2) which contains the data starting from 1 to n positions i want to refer first file if + ... (4 Replies)
Discussion started by: anurupa777
4 Replies

4. Shell Programming and Scripting

help! Pattern Matching in vi or sed

I have a bunch of conf files, that contain the fully qualified names of servers. I would like to be able to use some sort of pattern matching with sed or vi, or whatever, to pull out the fully qualified server names, and dump them in a file. It just needs to work across several unix os. So, I... (4 Replies)
Discussion started by: tabini
4 Replies

5. Shell Programming and Scripting

SED pattern matching help

Hello All, I have the following lines in a file <address location="test" ConnectionName="test" /> I want to replace the above lines by <address location="test123" /> I am usind SED and not able to remove the new line characters between the two lines. Can anyone please help... (4 Replies)
Discussion started by: ramk
4 Replies

6. Shell Programming and Scripting

sed pattern matching

Unfortunately this chap has been banned for some reason and I was looking forward to the resolution of his question: - https://www.unix.com/shell-programming-scripting/123118-append-position-28-33-a.html He was asking if you can use sed to match a pattern you want to replace within a... (6 Replies)
Discussion started by: steadyonabix
6 Replies

7. UNIX for Dummies Questions & Answers

pattern matching w/ unexpected characters

how do i check if the first character is a parenthese in pattern matching? if ] obviously doesnt work. How do I use it to compare so it doesnt try to use it to group. (5 Replies)
Discussion started by: questionasker
5 Replies

8. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

9. Shell Programming and Scripting

sed pattern matching

Hi, I have a file which contains a word like ravi and ravi30. i want to replace only the word ravi with xxx for that i am using the below sed command sed -e 's/ravi/xxx/g' . but the above command out put is xxx and xxx30 but i dont need to change ravi30 please guide me how to proceed.... (4 Replies)
Discussion started by: ravi_rn
4 Replies

10. Shell Programming and Scripting

Pattern matching sed

MSG="THERE WERE XX RECORDS IN ERROR TABLE,AAAA, WHEN LOADING THE BBBB TABLE WITH EXTRACT FROM CCCC INTO TABLES FOR , DATABASE DDDD." echo "$MSG" > /tmp/mplanmsg.$$.out I wan to replace XX with the content in $recordXX cat /tmp/mplanmsg.$$.out|sed 's/XX/\$recordXX/g'| sed... (3 Replies)
Discussion started by: leemjesse
3 Replies
Login or Register to Ask a Question