bash search and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash search and replace
# 1  
Old 03-20-2009
bash search and replace

Hello all,

I would like to replace some text that are forwarded in standard output from a script, then save the replaced text to a file.
The text i would like to replace is in the form of:

1 some text
1.1 other text
1.2 more text
1.2.1 still more text

i would like to replace
1 some text by T1 some text
1.1 other text by T2 other text
1.2 more text by T2 more text
1.2.1 still more text by T3 still more text
and so on....

Any help appreciated, thank you.
# 2  
Old 03-20-2009
I started with this :
Code:
first_script.sh | awk '/^([[:digit:]]|[[:digit:]].)+[[:blank:]]/ {print}'

Instead of printing i would like to replace as explain in the first post.
# 3  
Old 03-20-2009
I've got another solution but still dont know how to replace the text:
Code:
first_script.sh | while read line
        do
                isTitle=$(echo $line | grep "^[[:digit:]]")
                if [ "$isTitle" ]
                then
                        echo $line
                fi
        done

# 4  
Old 03-20-2009
ok, i almost done it:
Code:
while read line
        do
                isTitle=$(echo $line | grep "^[[:digit:]]")
                if [ "$isTitle" ]
                then
                        num=${line%%[[:blank:]]*}
                        echo $num
                        length=${#num}
                        if [ $length -eq 1 ]
                        then
                                title=T$length
                        else
                                echo
                        fi
                        echo "title : $title"
                fi
        done

just have to do the algorithm for determining the level of title: 1, 2, 3 ...
# 5  
Old 03-20-2009
Code:
while read line
        do
                isTitle=$(echo $line | grep "^[[:digit:]]")
                if [ "$isTitle" ]
                then
                        num=${line%%[[:blank:]]*}
                        echo $num
                        length=${#num}
                        level=$(echo $length * 1/2 + 1/2 | bc)
                        title=T$level
                        echo "title : $title"
                fi
        done

Can someone help on how to use bc ?? I have an error at the moment, it doesn't take the variable. How can i pass it ?
# 6  
Old 03-20-2009
Hammer & Screwdriver for you bc question

Code:
> length=12
> level=$(echo "${length} * .5 + .5" | bc)
> echo ${level}
6.5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - How to update header of scripts in one pass - multiline search/replace

Hello. A find command return a list of file. For each fileReplace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines ) by the following content (exactly) : §2019_08_23§ # # ... (8 Replies)
Discussion started by: jcdole
8 Replies

2. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

3. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

Can BASH handle mathematical operations and do a Search & Replace?

Hello, I have a bunch of xml file that needs to have edits made and I was wondering if a BASH script could handle it. I would like the script to look within my xml files and replace all integers greater than 5px with a value that is 25% smaller. For example, 100px = 75px. Since the integers... (12 Replies)
Discussion started by: jl487
12 Replies

6. Shell Programming and Scripting

Bash sed search and replace question

I have several files that I need to modify using sed. I know how to do that, but now a new requirement has come up. Now, I need to make changes to all lines that don't start with certain strings. For example, I need to change all lines except for lines that start with "a", "hello there",... (3 Replies)
Discussion started by: RickS
3 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

search and replace by bash

Hello everyone! I have a file: text.txt and also a list of replacement: replace.txt (tabbed) is isn\'t Mac Windows will won\'t \.\n \! <--- ! plus 2 spaces I want a script which will change text.txt as I made a script: #!/bin/bash echo Please input the file of... (2 Replies)
Discussion started by: Euler04
2 Replies

10. Shell Programming and Scripting

Help : Search and Replace

Hello, I'm lost on this one. I'm trying to search for a string in a file and replace it. But the string I'm searching for is on 2 seperate lines in the file and I only want to replace the 2nd occsrrance. This is what I know, but don't know how to select occurances that are on a seperate... (2 Replies)
Discussion started by: ctcuser
2 Replies
Login or Register to Ask a Question