File modification help: without manual editing through vi :


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File modification help: without manual editing through vi :
# 8  
Old 05-14-2013
Hi DGPickett,
Thanks for the explanation...

I tried the sed -i on Suse Linix, but it did not work..


Code:
# sed -i '/^INTERFACE_NAME\[8\]="lan5:5"$,/^INTERFACE_MODULES\[8\]=""$/d' file
sed: -e expression #1, char 34: unknown command: `^'

Am i missing anything, Thanks..
# 9  
Old 05-15-2013
Quote:
Originally Posted by rveri
Don,
>Are you saying that you:
- yes, this is corrrect .

1. want to find a line that contains lan5:5 ,
2. find a digit string between [ and ] on that line, and then
3. remove every line in the file that contains that digit string between [ and ] ?

Thank you..
Reveri.
OK. Doing exactly what you're requesting, I have an example that uses sed and ex. If you only need to delete lines that contain the digit string between [ and ] after finding lan5:5, I also have an awk example that does that. The following script includes both of these examples. Hopefully, one or both of these will do what you want:
Code:
#!/bin/ksh
# Usage: tester file pattern
# For the example given in this thread, file is the name of the input file and
# pattern is lan5:5.
if [ $# -ne 2 ] || ! cp $1 backup$$
then    printf "Usage: %s file pattern\n" ${0##*/} >&2
        exit 1
fi
# Gather operands:
file="$1"
pattern="$2"
printf "A backup copy of \"%s\" has been saved as \"%s\"\n" "$file" backup$$

# Try 1st example using sed to create a pattern to match and ex to remove lines
# containing that pattern.
echo 'using sed and ex:'
# Set RE to a BRE that will match "[digits]" where digits is the value found on
# the line that contains the given pattern inside double quotes, and print that
# BRE for reference..
RE="$(sed -n "s/.*[[]\\([0-9]*\\)[]].*\"$pattern\".*/[[]\\1[]]/p" "$file")"
printf "RE is \"%s\"\n" "$RE"

# Use ex to delete every line from file that matches the computed RE and check
# the exit status:
if ex -s "$file" <<-EOF
        :g/$RE/d
        :wq
EOF
then    # ex complted successfully, verify that we got the expected results:
        if diff "$file" expected
        then    echo 'Succeeded'
        fi
else    echo 'ex or sed error or no match found'
fi

# Print the updated file:
cat "$file"

# Now try 2nd example using awk:
echo
echo 'using awk:'

# Restore the given file to its original contents
cp backup$$ "$file"

if awk -v pat="\"$pattern\"" '
$0 ~ pat {
        # We found a line that contains the given pattern inside double quotes.
        if(match($0, /[[][0-9]+[]]/)) {
                # And we found a digit string between "[" an "]", create a
                # pattern to match that digit string and the "[" before it and
                # the "]" after it
                RE = "[[]" substr($0, RSTART + 1, RLENGTH - 2) "[]]"
                # Note that we have found the pattern and created an ERE to
                # match the correponding digit string in square brackets:
                found = 1       # Note that we have found the pattern and
                                # created an ERE to match the digit string.
        }
}
!found || $0 !~ RE      # print the current line if we have not found the
                        # pattern or have found the pattern but this line does
                        # not match the ERE.
END {   exit !found     # Set exit status: 0->success, 1->pattern not found
}' "$file" > _tmp$$
then    # awk completed successfully, cp results back to the input file and
        # remove the temp file:
        cp _tmp$$ "$file"
        rm _tmp$$
else    echo "awk failed or no match found"
fi
# Verify that we got what we expected:
if diff "$file" expected
then    # Success, print the updated file.
        echo 'Succeeded'
        cat "$file"
fi
if [ -f _tmp$$ ]
then    # awk failed, print the contents of our temp file:
        echo _tmp$$
        cat "_tmp$$"
fi

I use the Korn shell, but this will also work with bash or any other POSIX conforming shell. If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

If you save the above script in a file named tester, make it executable by running the command:
Code:
chmod +x tester

and then run the command:
Code:
./tester file "lan5:5"

(assuming that the input file you showed us in message #1 in this thread is stored in a file named file and the expected output shown in that message is stored in a file named expected), the output produced will be something like:
Code:
A backup copy of "file" has been saved as "backup94188"
using sed and ex:
RE is "[[]8[]]"
Succeeded
BROADCAST_ADDRESS[5]="10.140.236.255"
INTERFACE_STATE[5]="up"
DHCP_ENABLE[5]="0"
INTERFACE_MODULES[5]=""
ROUTE_DESTINATION[2]=default
ROUTE_GATEWAY[2]=10.40.118.4
ROUTE_COUNT[2]=1
INTERFACE_NAME[10]="lan5:7"
IP_ADDRESS[10]="10.140.239.62"
SUBNET_MASK[10]="255.255.255.0"
BROADCAST_ADDRESS[10]="10.140.239.255"
INTERFACE_STATE[10]="up"
DHCP_ENABLE[10]="0"

using awk:
Succeeded
BROADCAST_ADDRESS[5]="10.140.236.255"
INTERFACE_STATE[5]="up"
DHCP_ENABLE[5]="0"
INTERFACE_MODULES[5]=""
ROUTE_DESTINATION[2]=default
ROUTE_GATEWAY[2]=10.40.118.4
ROUTE_COUNT[2]=1
INTERFACE_NAME[10]="lan5:7"
IP_ADDRESS[10]="10.140.239.62"
SUBNET_MASK[10]="255.255.255.0"
BROADCAST_ADDRESS[10]="10.140.239.255"
INTERFACE_STATE[10]="up"
DHCP_ENABLE[10]="0"

The number after "backup" in the output shown above in orange, is the process ID of the shell running this script.

If you then copy the backup file created by this script back to your input file and run it again with something like:
Code:
./tester file "lan5:x"

it will display error messages indicating that it couldn't find lan5:x, show the updated input file produced by sed and ex, show the diffs between what awk script produced and what you wanted, and show the temp file created by awk.

If everything works as expected, remove the backup file after you have verified that your input file has been updated to contain what you wanted.

If the awk script fails, it may also leave around a file named _tmpdigits where digits is also the process ID of the shell running this script. If it was an actual awk error (instead of not finding the given pattern in your input file), this temp file may help you debug what went wrong.

I hope the comments in the script explain what is going on, but if part of it doesn't make sense, feel free to ask for an explanation of the parts you don't understand.

Hope this helps...
# 10  
Old 05-16-2013
Forgot / after first $. Sorry!

The index search is similar:
Code:
for dix in $(
 sed -n 's/^INTERFACE_NAME\[\([0-9]\{1,9\}\)\]="lan5:5"$/\1/p' yourfile
 )
do
 sed -i '/\['"$idx"'\]/d' yourfile
done

Narrative: sed no auto print mode find the key line and cut out the digits inside the square braces and print them to the for loop, which for each index found, removes all the lines indexed by that number in the file. Normally I use a while read, but since we are changing the file we are getting indexes from, I get all indexes before doing index deletes.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help For File modification

Hi, I have a file. File contains are as follows : Feb 19, 2012 5:05:00 PM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Feb 19, 2012 5:05:00 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 771 ms Feb 20, 2012... (3 Replies)
Discussion started by: mnmonu
3 Replies

2. Shell Programming and Scripting

File Modification

Hi, I have a file input.txt. cat input.txt output is as follows : Code: "0001"~"name"~"bb"~"20.25"~""~""~"0002"~"name" "dd"~"35.50"~"" ~""~"0003"~"name"~"aa"~"21.3 5"~""~""~ I want the output looking like: cat output.txt Code: "0001"~"name"~"bb"~"20.25"~""~""~... (6 Replies)
Discussion started by: mnmonu
6 Replies

3. Shell Programming and Scripting

Help for File Modification

Hi All, I have a file. This file contain huge amount of data. I want to modify this file. I want enter new line when count of "~ character is 79. Please find below the code : cat file_name | tr -d '\n' | sed... (6 Replies)
Discussion started by: mnmonu
6 Replies

4. Shell Programming and Scripting

Help for File Modification

Hi All, I have a file disk_space.log. cat disk_space.log 94% / 32% /boot 38% /mnt/data 100% /media/CDROM I want the output, like cat disk_space.log 94% / 100% /media/CDROM That means print the line those are grater-than 90%. And rest of the line is remove from file. I have a... (2 Replies)
Discussion started by: mnmonu
2 Replies

5. Shell Programming and Scripting

Help for File Modification

Hi, I have a file abcd.txt. cat abcd.txt output is as follows : "aa"~"bb"~"001"~""~""~"cc" "dd"~"005"~"" ~""~"kk"~"aa"~"00 8"~""~""~ I want the output looking like: cat abcd.txt "aa"~"bb"~"001"~""~""~ "cc""dd"~"005"~""~""~ "kk"~"aa"~"008"~""~""~ I have a script. (4 Replies)
Discussion started by: mnmonu
4 Replies

6. Shell Programming and Scripting

Help with file modification

Hi, I have a file test.txt . The contain of the file is as below : 365798~SAPUS~PR5~0000799005~ADM CHARG MEDCAL INS~~~~~~~~~~~~~~~~~~~~~~~~SLAC480 I want to modify this file. And file contain loking like "365798"~"SAPUS"~"PR5"~"0000799005"~"ADM CHARG MEDCAL... (6 Replies)
Discussion started by: mnmonu
6 Replies

7. UNIX for Dummies Questions & Answers

txt file modification which is beyond me

Dear all, I 'd like to create a new txt file using the old file. For example, in old file, if count=2 then in new file, repeat that row twice, with the only difference is: on the first row, 'start' column contains the 1st apart of the 'start' in the old file; while in the 2nd row, the 'start'... (7 Replies)
Discussion started by: forevertl
7 Replies

8. Shell Programming and Scripting

File modification history

Can anyone please suggest an alternate command for "stat" . I am trying this on Solaris 5.9 , but the command doesn't exist. Basically i need to see one particalar file modification history. Any help is appreciated. (4 Replies)
Discussion started by: mk1216
4 Replies

9. UNIX for Dummies Questions & Answers

How to change the file modification time of a file on nfs mount point

Hi I am accessing a file on nfs mounted device, after completing using of the file, i am tring to restore the access time and modification times of the file. So i got the previous modified time of the file using stat() function and trying to set the date and time for the file, To set these... (6 Replies)
Discussion started by: deepthi.s
6 Replies

10. Shell Programming and Scripting

File modification

Dear all, i have a file which contains this lines. 0-0 CC=1 0-01 0-011 0-0111 0-01110 F=500 CC=1 L=15 M=5 TRD=3948... (2 Replies)
Discussion started by: panknil
2 Replies
Login or Register to Ask a Question