Manipulate "&" in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulate "&" in sed
# 1  
Old 07-22-2011
CPU & Memory Manipulate "&" in sed

Is it possible to manipulate the ampersand in sed? I want to sum +1 to all numbers in a file. Example that doesn't work:
Code:
sed "s/[0-9]\{1,2\}/$(expr & + 1)/g" filename

Also, how to return 02 instead of 2 from expr?

Last edited by teresaejunior; 07-22-2011 at 03:43 AM..
# 2  
Old 07-22-2011
Quote:
Is it possible to manipulate the ampersand in sed?
No (for you purposes and easy enough). You should use awk or perl.
# 3  
Old 07-22-2011
Hello, yazu! What do you mean with "for my purposes"? Isn't is possible in any way to manipulate the ampersand?

Also, how could I do it in awk?

Thank you!
# 4  
Old 07-22-2011
Quote:
Originally Posted by teresaejunior
Is it possible to manipulate the ampersand in sed? I want to sum +1 to all numbers in a file. Example that doesn't work:
Code:
sed "s/[0-9]\{1,2\}/$(expr & + 1)/g" filename

Also, how to return 02 instead of 2 from expr?
Try as
Code:
echo '123'|sed "s/.*/expr & + 3/e"

# 5  
Old 07-22-2011
@michaelrozar17 almost there!

It works on a file with numbers only. The file has all kinds of content, and I want to replace only the numbers.

Code:
$ sed "s/[0-9]\{1,2\}/expr & + 2/e" filename

sh: Syntax error: "(" unexpected

sh: 01: not found

sh: Syntax error: "(" unexpected (expecting ")")

# 6  
Old 07-22-2011
Can you post few sample lines of your input file..?
# 7  
Old 07-22-2011
Here is a few lines:

Code:
$bbx01 = new GtkHButtonBox();
$bbx01->set_layout(4);
    $btn03 = GtkButton::new_from_stock(Gtk::STOCK_CANCEL);
    $btn03->connect_simple('clicked', array($wnd01, 'destroy'));
$bbx01->add($btn03);

    $btn04 = GtkButton::new_from_stock(Gtk::STOCK_OK);
    $btn04->connect_simple('clicked', 'put_contents', $wnd01);
$bbx01->add($btn04);

$vbx04->pack_start($ntb01);
$vbx04->pack_start($bbx01);
$wnd01->add($vbx04);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with... (6 Replies)
Discussion started by: Nexeu
6 Replies

2. UNIX for Beginners Questions & Answers

Echo "abc" | sed - r 's/a/&_&/

I want to know the working of & here step by step using sed command. (1 Reply)
Discussion started by: Preeti07
1 Replies

3. UNIX for Beginners Questions & Answers

what does "&" do in this sed command

I want to know the working of & here step by step using sed command: echo "abc" | sed - r 's/a/&_&/' (2 Replies)
Discussion started by: Preeti07
2 Replies

4. Web Development

How would I mod_rewrite "/~a1Pha" and "/=a1Pha" to "/paste/a1Pha.htm"? (internally & externally)

Basically I want to shorten URLs on my html pasting site (pasteht.ml), by using "/~a1Pha" instead of "/paste/a1Pha". The ID is 5 numbers and letters, both cases. For example: /~idnum serves /paste/idnum.htm /=idnum serves /paste/idnum.htm /paste/idnum redirects to /~idnum (to update any old... (0 Replies)
Discussion started by: phillips1012
0 Replies

5. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

10. UNIX for Dummies Questions & Answers

Run away "bootpgw" & "inetd"

Hello All. I'm get the following messages posted to the /var/adm/syslog file ever second and not sure on how to stop the process. May 14 15:50:52 a3360 bootpgw: version 2.3.5 May 14 15:50:52 a3360 inetd: /etc/bootpgw exit 0x1 As said about this gets logged every second only thing that... (4 Replies)
Discussion started by: cfaiman
4 Replies
Login or Register to Ask a Question