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 :
# 1  
Old 05-13-2013
File modification help: without manual editing through vi :

Hi Experts,

I want to edit a file from a selected pattern range, please advise how to accomplish from command line, without going vi and doing it manually:


portion of the file given below: (red want to remove and save )
1. I know the interface number is lan5:5 , that need to match first.
2. I want to get the pattern for this , i.e the instance number for the line containing lan5:5 is is [8] in this case.
3. I want to remove all the entries for instance number : [8]
Code:
 want to delete from line IP_ADDRESS[8]="10.140.220.62"  to  DHCP_ENABLE[8]="0"  line.



Code:
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[8]="lan5:5"
IP_ADDRESS[8]="10.140.220.62"
SUBNET_MASK[8]="255.255.255.0"
BROADCAST_ADDRESS[8]="10.140.220.255"
INTERFACE_STATE[8]="up"
DHCP_ENABLE[8]="0"
INTERFACE_MODULES[8]=""
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 output in the file should be like this after deleting the patterns:



Code:
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"


Thanks,

Last edited by rveri; 05-13-2013 at 06:30 PM..
# 2  
Old 05-13-2013
You problem statement is not clear. If you mean that want to delete every line from your input file that contains the string "[8]", the following script will remove that set of lines from the file named by the argument passed to the script:
Code:
#!/bin/ksh
ex -s "$1" <<-EOF
        :g/[[]8[]]/d
        :wq
EOF

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-13-2013
In a while read loop, you could
check the line for [8] then
echo line >> ip_stuff_file_name
otherwise
echo line >> not_ip_stuff_file_name
end check
done

move original_file_name to original_file_name_backup
move the not_ip_stuff_file_name to original_file_name



As usual, you want to run it on test data files only, until you are sure of the results.

There might be more elegant ways, but that's the first thing that came to mind.
# 4  
Old 05-14-2013
Don,
Thanks,
The desired script should find lan5:5 , and then to decide the [8] string, then remove all the lines having [8] entires. Can we put the condition for finding lan5:5 and then do the editing on the file to remove the lines.
- As without seeing the file I do not know if the lines I am going to remove has the pattern of [8] , but what I know is lan5:5 . Hope you got it.

Code:
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[8]="lan5:5"
IP_ADDRESS[8]="10.140.220.62"
SUBNET_MASK[8]="255.255.255.0"
BROADCAST_ADDRESS[8]="10.140.220.255"
INTERFACE_STATE[8]="up"
DHCP_ENABLE[8]="0"
INTERFACE_MODULES[8]=""
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"

# 5  
Old 05-14-2013
Quote:
Originally Posted by rveri
Don,
Thanks,
The desired script should find lan5:5 , and then to decide the [8] string, then remove all the lines having [8] entires. Can we put the condition for finding lan5:5 and then do the editing on the file to remove the lines.
- As without seeing the file I do not know if the lines I am going to remove has the pattern of [8] , but what I know is lan5:5 . Hope you got it.

Code:
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[8]="lan5:5"
IP_ADDRESS[8]="10.140.220.62"
SUBNET_MASK[8]="255.255.255.0"
BROADCAST_ADDRESS[8]="10.140.220.255"
INTERFACE_STATE[8]="up"
DHCP_ENABLE[8]="0"
INTERFACE_MODULES[8]=""
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"

I'm sorry, but I don't understand your requirements. It is obvious that we have a language barrier. Are you saying that you:
  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 ]?
# 6  
Old 05-14-2013
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.
# 7  
Old 05-14-2013
I wonder if GNU 'sed -i' would work for you. Note that vi in : modes is ex, vi is visual ex, and both vi and ex drive OK in scripts. sed is a stream version of ex. sed -i makes a temp file and then swaps files. (sed ... old_file >/tmp/xrandom ; mv -f /tmp/xrandom old_file)
Code:
sed -i '/^INTERFACE_NAME\[8\]="lan5:5"$,/^INTERFACE_MODULES\[8\]=""$/d'

To delete by index takes two passes, one to find the index and another to remove those lines.

Last edited by DGPickett; 05-14-2013 at 05:56 PM..
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