Sed-- command help required

Thread Tools Search this Thread
# 1  
Sed-- command help required

Hi Gurus,

I have a small requirement.
Let suppose i have a file test.txt
test.txt contains
Dispatched date = '2008-04-08'
Name = 'Logers'

Now i want to add one more line to it as Number of Responses = "$a"
$a will be chnaging dynamically which i had grepped it in the script.

Now i want to add this line to test.txt

suppose now a holds the value 20 my output should be

Dispatched date = '2008-04-08'
Name = 'Logers'
Number of Responses = 20

Can you help me out how to get the required solution by using sed command

I think it can be possible ny using sed 2 times Can anyone give me a example code to get the requirement done
# 2  
You can try like:
grep "Delete/Rename/Compress Log Files" test.txt  | awk '{print "Number of Responses " $1}' >>test.txt

Last edited by Yogesh Sawant; 03-18-2008 at 10:06 AM.. Reason: added code tags
# 3  
Originally Posted by koti_rama
You can try like:
grep "Delete/Rename/Compress Log Files" test.txt | awk '{print "Number of Responses " $1}' >>test.txt
What is "Delete/Rename/Compress Log Files" in the above command
What should be kept in place of that?
# 4  
Try this one:

awk '/Name = \047Logers\047/{print $0;print "Number of Responses = 20";next}1' test.txt

# 5  
Originally Posted by Franklin52
Try this one:

awk '/Name = \047Logers\047/{print $0;print "Number of Responses = 20";next}1' test.txt


But here no of responses = 20 is not constant one it will be a variable there i shouls get the value of variable a

# 6  
Pssandeep, please do not uppercase your reply or use IM-speak!

Here is a short example which shows you how to do what you asked for using sed


# number of responses

cat <<EOT >$TMP1
Dispatched date = '2008-04-08'
Name = 'Logers'

echo "====================="
cat $TMP1
echo "====================="

sed '$a\
Number of Responses = '"${A}"'
' $TMP1 > $TMP2

cat $TMP2
echo "====================="

rm $TMP1 $TMP2
exit 0

# 7  
Here's an example how you can use variables with awk:



awk -v name=$MyName -v num=$MyNum '
$0=="Name = \047"name"\047" {print $0;print "Number of Responses = "num;next}1
' test.txt


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #997
Difficulty: Medium
The use of hypervisor technology by malware and rootkits installing themselves as a hypervisor below the operating system is known as hyperjacking.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed pattern matching help required

Hi I am trying to pattern match with sed, if it finds a match I need it to insert characters at the 28th position, its working but its also adding an extra space and I don't know why, below is the code. sed 's/715023044/\n&/g' $asn | sed '/^71502304413-000/s/./B4/28' | sed -e :a -e... (4 Replies)
Discussion started by: firefox2k2
4 Replies

2. Shell Programming and Scripting

Tricky sed required

Hi All I need to put some sed together for a task and its a bit advanced for me, so I thought I'd ask if anyone here could help. I have a csv file with content like this - "","abcde","","" "'","abcde","","" "","","","1234" "'e'","","","" I need to remove any single quotes that fall... (17 Replies)
Discussion started by: steadyonabix
17 Replies

3. UNIX for Dummies Questions & Answers

Clarification required on sed

Hi Can some one tell what does this sed command do sed 's/*$//g I am more curious on the highlighted part , can some one explain what does that mean. Thanks Sri (1 Reply)
Discussion started by: Sri3001
1 Replies

4. Shell Programming and Scripting

sed help required

Hi All, I have one file with below type of data in it, $ cat test.txt ###123 ###xyxytuerwb ###2 ###tyupe Here I would like to replace all the characters with "x" after the 3 "###" with the same number of characters. Can you please help me to achieve this. (7 Replies)
Discussion started by: gr8_usk
7 Replies

5. Emergency UNIX and Linux Support

Complicated SED search required

Hi All, I'm trying to extract all the description fields from a MIB file which contain multiple instances of the following text: ENTERPRISE compaq VARIABLES { sysName, cpqHoTrapFlags, cpqSsBoxCntlrHwLocation, cpqSsBoxCntlrIndex, cpqSsBoxBusIndex,... (10 Replies)
Discussion started by: badoshi
10 Replies

6. Shell Programming and Scripting

Help required with awk/sed

Hi I have a file, with format like: column1|coulumn2|column3|column4 A|X|K|18 L|O|R|31,42,25 G|H|I|55,66 L|E|Q|25,31,94 output required: column1|coulumn2|column3|column4 A|X|K|18 L|O|R|31,25 L|E|Q|25,31 Input File Format: All columns are seperated using |, last column... (8 Replies)
Discussion started by: New to awk
8 Replies

7. Shell Programming and Scripting

Help required in sed or awk.

Hi All, I need to pick up data on both sides of "=" sign. For eg, following is the context that I have. Can I have the ip address in one variable and machine name in another variable using sed or awk. ... (1 Reply)
Discussion started by: nua7
1 Replies

8. Cybersecurity

Help Required: Command to find IP address and command executed of a user

Hi, I am trying to write a script which would figure out who has run which command and their IP. As i dont have any clue as to which commands would do this job, i request some gurus to help me on this. Thanks Vishwas (2 Replies)
Discussion started by: loggedout
2 Replies

9. Shell Programming and Scripting

Help in sed required.

Hi All, I am facing a small problem in sed. I want to insert a line in the existing file. Existing code: access to attr=userPassword by self write by * auth access to * by self write by users read by anonymous auth Desired code: access to attr=userPassword by self... (14 Replies)
Discussion started by: nua7
14 Replies

10. Shell Programming and Scripting

sed script required

Collegues I have a flat file with data in following structure. (S1 (S (NP (NP (DT The) (JJ beautiful) (NN view)) (PP (IN of) (NP (JJ outside) (NNP greenery)))) (VP (VBZ adds) (NP (DT a) (NN tranquil) (NN touch)) (PP (TO to) (NP (DT this) (NN place)))) (. .))) I have to extract the contents in... (2 Replies)
Discussion started by: jaganadh
2 Replies

Featured Tech Videos