Scratching my head over old post


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scratching my head over old post
# 1  
Old 09-08-2017
Scratching my head over old post

Hello !

Pondering over some old post & I need some help.

Here is the link below -

When I run this,

Code:
echo " process(130) Deleting Text on line 11 (ESN:27723211621B01DJ68AG) because a number wasnt 'AVAILABLE'  and is not found in the database" | awk '$0=$2' FS=\( RS=\) "

I get the following output
Code:
130 
ESN:27723211621B01DJ68AG

Next,

Code:
echo " process(130) :27723211621B01DJ68AG) because a number wasnt 'AVAILABLE'  and is not found in the database" | sed "s/^.*(\([^)]*\)).*(....\(.*\)).*'\(.*\)'.*/\1 \2 \3/"

130 27723211621B01DJ68AG AVAILABLE

Questions -
1. In there , the magic is happening all because of $0=$2. Can someone please explain to me as to what does $0=$2 means ?
2. Also can someone go over the sed operation here as I tried to understand this for the past 2 hours and I am not getting anywhere with this.


best regards,
Lee.
# 2  
Old 09-08-2017
Quote:
Originally Posted by OMLEELA
Hello !

Pondering over some old post & I need some help.

Questions -
1. In there , the magic is happening all because of $0=$2. Can someone please explain to me as to what does $0=$2 means ?
2. Also can someone go over the sed operation here as I tried to understand this for the past 2 hours and I am not getting anywhere with this.


best regards,
Lee.
In awk $0 refers to the whole record (line) while $1, $2 ... refer to the individual fields. So $0=$2 simply replaces the whole record with field 2.

In sed the (escaped) parentheses earmark part of a pattern for a later reference.

Take for instance this:
Code:
$ echo "charlie farley" | sed 's/\(charlie\) .*/\1 chaplin/'
charlie chaplin
$ echo "piggy malone" | sed 's/\(charlie\) .*/\1 chaplin/'
piggy malone

So basically I want to change "charlie farley" into "charlie chaplin" (or any other charlie) but leave anything else alone. I this extremely simple example I could have written
Code:
$ echo "charlie farley" | sed 's/charlie .*/charlie chaplin/'

But consider this
Code:
$ echo "charlie farley" | sed 's/\(charl.*\) .*/\1 chaplin/'

Now I can catch "charles" or "charley" too. More general.

The \1 refers to the first earmarked pattern. But what if I do two of them?
Code:
$ echo "Charles Chaplin" | sed 's/\(.*\) \(.*\)/\2, \1/'
Chaplin, Charles

This last example takes two strings separated by a space and reverses them, putting a comma between.

Hope that helps.

Andrew
# 3  
Old 09-08-2017
1) awk with RS set to ) doesn't break records at line ends but on occurrences of the RS char / pattern. So, the first record (also referred to as $0 in awk) is
Code:
 process(130

, which is split in two at the field separator FS=\(.
Code:
$0=$2

then overwrites the entire record with its second field, and, because used as an awk-pattern not equal 0 (= TRUE), takes the default action print.

2) Your echotext is corrupted in your post as the second left parenthesis is missing. The sed substitutes the BRE
Code:
^               start of line
.*(             (.) any character (*) zero or more times up to a left parenthesis
\([^)]*\)       first sub-expression (delimited by escaped! parentheses): zero or more non-right-par.
).*(....        next right par. followed by any char zero + times, then left par., then any four chars
\(.*\)          second sub-expr.: any chars zero or more times
).*'            next right par. followed by any char 0+ times, then apostrophe (single quote)
\(.*\)          third sub-expr.: any chars 0+ times
'.*             rest of line starting from apostrophe

by first sub-expr. space second sub-expr. space third sub-expr.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff/head - not sure if this is the right command to use

Hi, I need some advise on whether there is a better way of doing what I am currently planning to do. Perhaps I should be using arrays instead of re-directing output to files? I need to use a tool/program named ADRCI provided by Oracle to remove trace files that it generates. Honestly it is... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

find and head -1

i have lots of files in /law/prod and /law/dev, such as AP20PD, AP20WS, AP20.scr, AP20.rpt if i am in /law DIR find . -name AP20PD, found in /law/prod and /law/dev i want to head -1 AP20PD from both location and >> /tmp/test.log can i use find and head in one line ? ----------... (1 Reply)
Discussion started by: tjmannonline
1 Replies

3. Shell Programming and Scripting

head followed by variable??

I know that the common use of head is for example head -3 etc.Is there any possibility that,if i have a variable that equals to an integer(i=5),i can write head -i?? If not,what syntax or commands should i write down in order to have the same result? //maybe something lik head -"$variable" ? (2 Replies)
Discussion started by: bashuser2
2 Replies

4. Shell Programming and Scripting

Search HEAD of CVS

Im looking for a 'relatively' easy way to search through cvs to look for a particular string in the HEAD revisions. I realize the way CVS stores versions makes this difficult. But I'm trying to come up with some script to allow this search (performance is not expected here). Currently this... (0 Replies)
Discussion started by: bobtheowl2
0 Replies

5. Shell Programming and Scripting

head usage

$ct=1 head -n $ct file. When i used like this, i got an error , Bad usage of head Cant we use variables in place of number in HEAD. In my requirement for every iteration i should increase the number in Head and tail the last one. HOw can i achieve this (5 Replies)
Discussion started by: vasuarjula
5 Replies

6. Shell Programming and Scripting

Head and Tail in One Line

I am new to UNIX......I have one file which contains thousnads of records with header and tailer. Header Record 1 Record 2 .... .... Last Record Trailer I want to concatenate Header and Trailer in the first line....now the output should look like this: Header: Header value, Trailer:... (2 Replies)
Discussion started by: 33junaid
2 Replies

7. Shell Programming and Scripting

head command

Hi All, How can the head command be used to extract only a particular line. By default head -n filename displays the first n lines. I want only the nth line. I couldn't get it from forum search. Thanks, Sumesh (6 Replies)
Discussion started by: sumesh.abraham
6 Replies

8. UNIX for Dummies Questions & Answers

head & tail

I've a major file which includes other files and now I wanna 'cut' the file in several minor parts....like .... find / -name "*.tmp" >filea wc -l filea >fileb sed -e '1s/ filea//' fileb >filec AMOUNT=`cat filec` if ; then cat file a |head -100l (ell) |tail -100l >filec cat file a |head... (6 Replies)
Discussion started by: svennie
6 Replies

9. UNIX for Dummies Questions & Answers

help.. I am in way over my head !!!!

my boss has done it again I have been sent to fix a unix issue and I ma hoping you can help three issues 1st. I have a printer that when you try to print to it the print job comes out on a diffrent printer. If I take the printer ( dot matrix thourgh a serail connection) to a diffrent local the... (3 Replies)
Discussion started by: oberon42
3 Replies
Login or Register to Ask a Question