SED: Combine text between two pattern in same line - sed

I m trying to use sed /awk command to parse my text file.
I want to print text between two pattern in same line with some character added in it.
ex:
If my line is
uint8_t aucRepresentationName[NR_UNIT_REPRESENTATIONS][CHARS_P_REPRESENTATION];
OR
uint8_t aucRepresentationName [NR_UNIT_REPRESENTATIONS] [CHARS_P_REPRESENTATION];
I want to print NR_UNIT_REPRESENTATIONS*CHARS_P_REPRESENTATION
So , I have stared with sed command to insert line deliminter \n after '[' , deviding line in suitable species and then tried to parse it again.
echo "bla bla bla [AK] bla bla bla bla [A_K] bla bla bla" | sed 's/\[/\n&/g;' | awk '{sub(/.*\[ /,"");sub(/\].*/,"");print;}'
echo "bla bla bla [AK] bla bla bla bla [A_K] bla bla bla" | sed 's/\[/\n&/g;' | sed -e 's/^.*\[ //g;s/ \].*$//g'
And it is not always two diamensional array, it could be none or single/double dimentional array, I need to first how many instance of [], if its 0 then write 1, if its single diamensional array then ex A[SIZE] then write SIZE its two diamensional array ex:A[RAW][COL] write RAW*COL..
I would like to know what is wrong in my command? or any other option to do it as it will help me to further study sed.
Amruta

Try:
awk -F '[][]' '{print $2,$4}' OFS=\* file
OK, if it is a single occurrence of 0,1 or two-dimensional in one page you could try:
awk -F '[][]' '{if(NF==3)print $2; else if(NF>4)print $2,$4; else print 1}' OFS=\* file
or less comprehensible ;) :
awk -F '[][]' '{$0=NF==3?$2:NF>4?$2 OFS $4:1}1' OFS=\* file

use this:
perl -lne 'push #a,/\[([^\]]*)\]/g;END{print join "*",#a}'
modified from here
tested below:
> echo "bla [AK] bla [A_K] 10" | perl -lne 'push #a,/\[([^\]]*)\]/g;END{print join "*",#a}'
AK*A_K

You can try the following:
echo "bla bla bla [AK] bla bla bla bla [A_K] bla bla bla" | sed 's/.*\[\(.*\)\].*\[\(.*\)\].*/\1*\2/g'
AK*A_K

With sed:
sed -n 's/[^]]*\[\([^]]*\)\]\s*\[\([^]]*\)\].*$/\1*\2/p' input

Related

Wrapping each line inside quotation marks with AutoHotkey

I have two thousand lines of text. I need each line to be wrapped inside quotation marks like "example".
Before
line 1
line 2
line 3
After
"line 1"
"line 2"
"line 3"
How could I take care of this with AutoHotkey?
Like this?
text=
(
bla bla bla
blah blah blah blah
blablah blablah blablah
)
MsgBox % RegExReplace(RegExReplace(text,"`am)^.","""$0"),"`am).$","$0""")

SED renaming with unknown amount of characters before a "

I have a file1 that has some PHP code in it. I need to find the following: action="blahblah" and replace it with action="error.php". Problem is, I don't know how many characters are between the quotes in the original.
Here's what I have that doesn't work:
sed 's:action="^[^"]*":action="error.php":' <file1> file2
How can I do this?
Why have you got the ^ start-of-line marker before the character class? Try it with:
sed 's:action="[^"]*":action="error.php":' <file1 > file2
Here's a transcript showing your version alongside that correction:
pax$ echo 'blah action="something" blah' | sed '
...$ s:action="^[^"]*":action="error.php":'
blah action="something" blah
pax$ echo 'blah action="something" blah' | sed '
...$ s:action="[^"]*":action="error.php":'
blah action="error.php" blah

sed/awk + line manipulation

I have two different questions(on both questions need to ignore spaces)
How to print all lines until "=" separator for example
echo " bla bla girl man dog = black white color bla 123 4" | sed/awk .....
Will print:
bla bla girl man dog
the second question
How to print all lines from "=" until end of line
echo " bla bla girl man dof = black white color bla 123 4" | sed/awk .....
Will print
black white color bla 123 4
THX for help
Lidia
You can try this:
echo " bla bla girl man dog = black white color bla 123 4" | awk -F '=' '{print $1}'
cut can help you.
cut cuts a line using a custom delimiter, and gives you any parts around it.
echo " bla bla girl man dof = black white color bla 123 4" | cut -d= -f1 gives you the first part before a =
echo " bla bla girl man dof = black white color bla 123 4" | cut -d= -f2 gives you the second part after a =
if you have more than one = one the line -f2- will give you everything after the first = (ignoring the second =).
Answer to the first question:
sed 's/^ *\([^=]*\) *= *\(.*\) *$/\1/'
Answer to the second question:
sed 's/^ *\([^=]*\) *= *\(.*\) *$/\2/'
Explanation:
^ *\([^=]*\) *= *\(.*\) *$ matches the whole line. It contains two capturing groups: first group captures everything up to the first = except boundary spaces, second group captures everything after the first = except for boundary spaces. \1 and \2 are references to the capturing groups.
EDIT The above answer to the first question is incorrect, as it leaves trailing spaces. A correct version should read:
sed 's/^ *\([^=]*\) *= *\(.*\) *$/\1/' | sed 's/ *$//'
There seems to be no way to make the [^=]* reluctant, hence two sed commands.
For the first, use
cut -d= -f1
For the second, use
cut -d= -f2-
But these solutions preserve spaces.
With sed:
First question:
echo " bla bla girl man dog = black white color bla 123 4" | sed -n '{s/\([^=]*\)=.*/\1/p}'
Second question:
echo " bla bla girl man dog = black white color bla 123 4" | sed -n '{s/[^=]*=\(.*\)/\1/p}'
With awk:
First question:
echo " bla bla girl man dog = black white color bla 123 4" | awk -F= '{print $1}'
Second quetion:
if you have exactly one = in each line:
echo " bla bla girl man dog = black white color bla 123 4" | awk -F= '{print $2}'
Using cut
echo " bla bla girl man dog = black white color bla 123 4" | cut -d= -f1
With Atmark its much more easy:
first
# split = head trim_
second
# split = tail trim_

sed + need to remove each line in file that begin with ###

How to remove line that begins with three #
For example need to delete all the following lines: from file
1 ### bla bla bal
2 ###blablabla
3 ### blabla
.
.
.
THX
Yael
cat file | sed '/^###/d'
you can use awk as well
awk '!/^[ \t]*###/' file

echo nested quotation marks in tcsh

I have a tcsh script that generates a text file. One of the lines in the text file is:
bla bla bla 'foo foo foo "bar bar bar"': etc etc;
Note the nested ' and " and also the : and ; that must be there.
The : and ; require the whole string to be surrounded by quotation marks. However, if I do that, I have trouble escaping the quotation marks.
The command is:
echo "bla bla bla 'foo foo foo "bar bar bar"': etc etc;" >> outfile
How can I escape the quotation marks around bar bar bar so that they get printed correctly?
echo "bla bla bla 'foo foo foo "\""bar bar bar"\""': etc etc;"
or this:
echo "bla bla bla 'foo foo foo "\"bar bar bar\""': etc etc;"
These should work for the simple example you gave, but may not help for what you're actually trying to do... Quoting in tcsh always annoyed me, especially when trying to define aliases with a mix of back-ticks, quotes, and double-qutes.
Be warned that the second form works for echo, but it actually creates three separate arguments on the command line, which are (after interpreting the escape sequences):
bla bla bla 'foo foo foo "bar
bar
bar"': etc etc;
The first form is the one you should use.