sed command not getting the right output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command not getting the right output
# 8  
Old 01-13-2016
Yes, "^" in a bracket expression is a negation. man regex:
Quote:
A bracket expression is a list of characters enclosed in "[]". It normally matches any single character from the list (but see below). If the list begins with '^', it matches any single character (but see below) not from the rest of the list.
The "g" flag is needed as we (hopefully) have multiple (2) matches that all need to be replaced.
# 9  
Old 01-13-2016
Quote:
Originally Posted by RudiC
Yes, "^" in a bracket expression is a negation. man regex:The "g" flag is needed as we (hopefully) have multiple (2) matches that all need to be replaced.
Thank you, but do you mind taking a look at my code and tell me whats wrong with it and how id fix it ?
# 10  
Old 01-13-2016
As MadeInGermany said, it's the greedy .* that makes it output the LAST combination of -<>: chars. On top, in the bracket expression, there's a "," too many. Your (modified) code snippet works:
Code:
sed -ne 's/[^<>:-]*\([-<>:]*\).*/\1/;p' file
<-:::
<-::
<-:::
->:::
->::

# 11  
Old 01-13-2016
Quote:
Originally Posted by RudiC
As MadeInGermany said, it's the greedy .* that makes it output the LAST combination of -<>: chars. On top, in the bracket expression, there's a "," too many. Your (modified) code snippet works:
Code:
sed -ne 's/[^<>:-]*\([-<>:]*\).*/\1/;p' file
<-:::
<-::
<-:::
->:::
->::

Got it. Do you mind looking at this ? its probably easy for you to solve (thread-related).

This code :
Code:
sed -e 's/[^-><:]*\([-<>]*[:]*\).*\[-<>]*[:]*/\1/;h' datasets/q13data.txt

Gives me this output :
Code:
"Oct<-:::NULL<-:: phi
SunSat<-::3.1415<-:::`Nov
WedBlue<-::Mar->:::OctJun

You see, there are 2 [<>-:] patterns here, one in the begining and one close to the EOL.

How would I filter those two out ? Id like to use grouping here if possible

EDIT: I changed the code to :
Code:
sed -ne 's/[^-><:]*\([-<>]*[:]*\)[^-><:]*\([-<>]*[:]\)[^-><:]*/\1\2/;p' datasets/q13data.txt

and get this output

Code:
->:::<-::: `
->::->::2.8chi
<-:::->:::3.14
<-:::<-:: phi
<-::<-:::`Nov
<-::->:::OctJun

How to filter out the last part :P?

Last edited by jozo95; 01-13-2016 at 06:30 PM..
# 12  
Old 01-13-2016
Try
Code:
sed 's/[^<-]*\([-<>:]*\)[^<-]*\([-<>:]*\).*/\1 \2/' file
<-::: <-:::
<-:: <-:::
<-::: <-:::
->::: <-:::
->:: ->:::

This User Gave Thanks to RudiC For This Post:
# 13  
Old 01-14-2016
Quote:
Originally Posted by RudiC
Try
Code:
sed 's/[^<-]*\([-<>:]*\)[^<-]*\([-<>:]*\).*/\1 \2/' file
<-::: <-:::
<-:: <-:::
<-::: <-:::
->::: <-:::
->:: ->:::

wow... perfect my friend, thank you very much!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Command Output

I want to know how this command output will be sed 's/^"\(.*\)"$/\1/' <filename> (3 Replies)
Discussion started by: abhilashnair
3 Replies

2. Shell Programming and Scripting

sed command is saving output as blank file

Hi, I am working on a script where I am adding adding colors to few of the info in the output. Now , after that is done , I see colour codes in log files which I don't want to see.:mad::mad::mad::mad: So , I tried using sed command in script as below which gives me o/p (new.log) as blank file... (7 Replies)
Discussion started by: Dream4649
7 Replies

3. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

4. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

6. Shell Programming and Scripting

How to append a value to the output after using sed command?

Hi All, I have a file where I am converting newlines to comma separated values but I would like to append zero if the output is empty Here is the command I am using sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' test1.txt test1.txt will have comma seperated values but sometimes this file can be... (6 Replies)
Discussion started by: rajeevm
6 Replies

7. Shell Programming and Scripting

Abnormality while piping tr command output to sed

i have a file seperated each line seperated by newline. For example alpha beta gamma i am trying to replace the newlines to "," but dont want , present at the end of the line so i am trying the below one liner . but not sure whats wrong but its not working cat myfile | tr -s '\n' ',' | sed... (9 Replies)
Discussion started by: chidori
9 Replies

8. Shell Programming and Scripting

Help needed with file output awk sed command - please

Hi I have a file that contains lines starting with a particular string plus a Colon: I need to output all these lines but only what comes after the colon Can you pelase assist? Example of lines in the file: com.ubs.f35.cashequities/cashequities: 1 2 ... (5 Replies)
Discussion started by: mnassiri
5 Replies

9. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

10. UNIX for Dummies Questions & Answers

Using grep output as input for sed command

Hi, I would like to know if this is possible, and if so what can i do to make this work. I would like to grep a line X from fileA and then use the output to replace a word Y in fileB. grep "line X" fileA | sed -e 's/Y/X/g' > outfile this statement does not work, as i do not know how to... (7 Replies)
Discussion started by: cavanac2
7 Replies
Login or Register to Ask a Question