LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2012, 12:08 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Red face wrong sed operation


Code:
rec=`$CCRCQUERY -id -engineer -createdt -checkindt $CCRNum`
tHE CODE GIVES THE FOLLOWING VALUE TO THE REC VARIALBLE:

ID |ENGINEER|CREATEDT |CHECKINDT
-------------|--------|----------|----------
CCMPR00684153|vijays |06-29-2009|07-06-2009

NOW I WISH to extract engineer cretedt and chedindt from rec.

Im using the following code to do so
Code:
engr=`echo $rec | awk -F\| '{print $2}' | sed 's/ //g'`
createdt=`echo $rec | awk -F\| '{print $3}' | sed "s/ //g"`
checkindt=`echo $rec | awk -F\| '{print $4}' | sed 's/ //g'`
but the value of engr is coming ENGINEER but i want vijays
value of createdt is coming CREATEDT but i want 06-29-2009 and simillarly for checkindt.

PLEASE HELP ME PLEASEEEEEEEEE
 
Old 08-28-2012, 12:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the problem is that you got 3 lines, awk parses the first line, not the third. You would need another awk:
Code:
awk -F\| ' NR == 3 { gsub (" ", ""); print $2 } '
you do not need sed, gsub will remove spaces

(not tested)
 
Old 08-28-2012, 12:36 AM   #3
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
IF I USE
Code:
 rec=`$CCRCQUERY -id -engineer -createdt -checkindt $CCRNum | grep CCMPR`
The rec varialble doesnt have any value.... why does it not grep the lower line?
 
Old 08-28-2012, 11:47 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Actually, we don't really need to use any external tools at all to do this.

Code:
rec='ID |ENGINEER|CREATEDT |CHECKINDT
-------------|--------|----------|----------
CCMPR00684153|vijays |06-29-2009|07-06-2009'

shopt -s extquote			#needed for the next line
rec="${rec##*$'\n'}"			#removes everything up to the final newline

IFS='|' read -r id engr createdt checkindt <<<"$rec"	#read with the IFS setting splits the remaining line directly into the desired variables (plus the unneeded id field).
This version uses parameter substitution and the bash/ksh specific ansi-c style quoting (along with the extquote shell option, necessary when you want to expand them inside parameter substitutions).

It also uses a here string to pass the variable to read.


If you need posix-style portability, we can use a simple loop instead.

Code:
while IFS='|' read a b c d; do
	id=$a
	engr=$b
	createdt=$c
	checkindt=$d
done <<HEREDOC
$rec
HEREDOC
The here document provides the input to the loop, after expanding the variable. Since all we're interested in is the last line, we only need to iterate over all lines, and only the final values will remain in variables when the loop terminates.
 
1 members found this post helpful.
Old 08-30-2012, 07:15 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
BTW, here's an even quicker way to target only the last line, if you are using a recent version of bash (v4+).

Code:
mapfile -t rec <<<"$rec"	#convert the variable to a line-separated array.
IFS='|' read -r id engr createdt checkindt <<<"${rec[@]: -1}"	#process the final entry.
From version 4.2 you can replace the last part with a negative array index: e.g. "${rec[-1]}"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] .\+ in sed: What am I doing wrong? kmkocot Linux - Newbie 5 07-09-2013 02:33 PM
[SOLVED] Sed with AND operation subashinik Linux - Newbie 14 06-30-2010 04:13 AM
sed command error - what am i doing wrong? Morrighan Linux - Newbie 8 06-15-2008 11:12 AM
Something is going wrong with the webcam operation Cyhaxor Slackware 0 04-18-2008 10:48 AM
hard drive size wrong in Windows after knoppix operation simbobo Linux - Hardware 5 08-22-2006 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration