Visit Our UNIX and Linux User Community


Sed or Awk or both to edit file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed or Awk or both to edit file
# 1  
Old 10-13-2009
Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name?

inputfile:
Code:
=======================
# comment
# comment
# comment

5   8   10   /tmp

5   8   10   /var/run

5   8   10   /etc/vfstab

5   8     9   /var/tmp

5   8   10   /var/adm/messages

=======================

Desired out put: change all numbers to "1 0 0" and remove lines which contain a file name
Code:
=======================

# comment
# comment
# comment

1    0    0   /tmp

1    0    0   /var/run

1    0    0   /var/tmp

========================

My current approach have not addressed "removing line which contain a file name.
Code:
#!/bin/ksh
######################################################################
#CREATION DATE: 10/05/2009
#AUTHOR: Jan De Wulf
#SCRIPT TITLE: correct_tivoli.ksh
#PURPOSE: adjust thresholds in /opt/Tivoli/lcf/dat/1/.dm/diskmon/diskmon.cfg
#USAGE: run on target server
#NOTES: Reduces the number of needless Tivoli "disk space threshold alerts"
#MODIFIED:
######################################################################

#====== VARIABLES ========
=/opt/Tivoli/lcf/dat/1/.dm/diskmon/diskmon.cfg
SED=/sysadmin/bin/tivoli_adjust.sed
#====== VARIABLES ========

if test -s $X; then
   cp -p $X $X.`date +%Y%m%d`
########## Set DISCOVERY to OFF and set all thresholds to "1   0   0"
   sed -f $SED $X > /tmp/$Y.`date +%Y%m%d`
     if test -s /tmp/$Y.`date +%Y%m%d`
      then
         if cmp -s $X /tmp/$Y.`date +%Y%m%d`
         then
            echo "file not changed:"
            rm $X.`date +%Y%m%d`
         else
            cp /tmp/$Y.`date +%Y%m%d` $X
            echo "Editing of file completed"
         fi
      else
       echo "Sed created an empty file"
     fi
   rm /tmp/$Y.`date +%Y%m%d`
else
   echo "original file is blank"
fi
bash-3.00#

Code:
#
######################################################################
#CREATION DATE: 10/05/2009
#AUTHOR: Jan De Wulf
#SCRIPT TITLE: tivoli_adjust.sed
#PURPOSE: sed script to correct /opt/Tivoli/lcf/dat/1/.dm/diskmon/diskmon.cfg file
#USAGE: Used by other script or on the command line "sed -f tivoli_adjust.sed xxx
#        where xxx = file name to be edited
#NOTES:
#MODIFIED:
######################################################################


s/DISCOVERY=ON/DISCOVERY=OFF/g
{
s/[01-9].[0-9].[0-9][0-9]/1     0       0/g
s/[01-9].[0-9].[0-9]/1  0       0/g
}
bash-3.00#


Last edited by Arsenalman; 10-13-2009 at 03:37 PM.. Reason: Poorly worded post
# 2  
Old 10-13-2009
Thanks Pludi for fixing my post and showing me the ropes.
# 3  
Old 10-14-2009
Quote:
Originally Posted by Arsenalman
What is an efficient way to remove all lines from the input file which contain a file name?
A point to start from (presumably more understandable than "geeky efficient", though Smilie):

Code:
cat in.file | while read LINE
do
  ENTRY=$( echo $LINE | sed 's/\([ \|0-9]\)//g' )
  if [ -f $ENTRY ]
  then
    echo "'$ENTRY' refers to file"
  elif [ -d $ENTRY ]
  then
    echo "'$ENTRY' refers to folder"
  fi
done

# 4  
Old 10-14-2009
Dr House thank you, its getting me further. Was not familiar with the shell scripting syntax of the file and directory test. The sed string works just about perfect, however as it strips all numbers this creates a problem with file names which contain numbers. I am currently going down the awk path however i am not there yet. Again thank you for your input

Quote:
12 BEGIN {
13 system("clear")
14 print "awk script start"
15 }
16 # match($4,) != 0 { print $0}
17 {
18 if ($1 == "#" || $1 < 0 || $1 == "D"*)
19 # if (NF != 1 )
20 # if (NF == 4 && $1 != "#")
21 # if ($1 == # || $1 == '')
22 # "#" { print $0 }
23 # if ($1>0-9)
24 { print $0
25 }
26 }
27 END {
28 print " "
29 print "awk is done"
30 }

Last edited by Arsenalman; 10-14-2009 at 06:12 PM.. Reason: was missing an entry
# 5  
Old 10-15-2009
Quote:
Originally Posted by Arsenalman
The sed string works just about perfect, however as it strips all numbers this creates a problem with file names which contain numbers.
Code:
[house@leonov] echo "1 2 3 /0tmp0" | sed 's/\([ \|0-9]\) //g'
/0tmp0

# 6  
Old 10-19-2009
Dr.House finally hit the jack pot. again thank you for your contribution which help me move forward along to my destination:

Code:
{
if (sub(/DISCOVERY=ON/, "DISCOVERY=OFF"))
print $0
if ( $1 ~ /^#/ ) {
        print $0 }
if ( NF == 4 ){
    if ( system("test -d " $4) ) {
         }
    else {
        print "1        0       0       "$4 "\n" }
}
}

# 7  
Old 10-20-2009
bash
Code:
#!/bin/bash
while read -r a b c d
do
 case "$d" in
    */* ) 
        if [ -f "$d" ];then
           continue
        fi
        ;;
 esac
 echo "$a $b $c $d"
done < "file"


Previous Thread | Next Thread
Test Your Knowledge in Computers #856
Difficulty: Medium
React was created by Jordan Renke, a software engineer at Facebook.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated. My example: sed -i '14 i\ # add these lines add these lines to... (5 Replies)
Discussion started by: Nvizn
5 Replies

2. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

3. Shell Programming and Scripting

Edit a file using awk ?

Hey guys, I'm trying to learn a bit of awk/sed and I'm using different sites to learn it from, and i think I'm starting to get confused (doesn't take much!). Anyway, say I have a csv file which has something along the lines of the following in it:"test","127.0.0.1","startup... (6 Replies)
Discussion started by: jimbob01
6 Replies

4. Shell Programming and Scripting

Inline edit using sed / awk

Hi, I have file with all the lines as following format <namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding" name="ENV_CONFIG_PATH" nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH" stringToBind="test"/> I want to replace (all the lines) value of... (8 Replies)
Discussion started by: shuklaa02
8 Replies

5. Shell Programming and Scripting

edit field using sed or awk

please help me to edit the second field using awk or sed i have input file below aa1001 000001 bb1002 000002 cc1003 000003 so i want the output file like below aa1001 01 bb1002 02 cc1003 03 (38 Replies)
Discussion started by: zulabc
38 Replies

6. Shell Programming and Scripting

what is the switch to let sed edit and save file

I remember there is a sed switch i can use to edit and save the file at the same time, but i cannot recall it at all. so instead of -> sed 's/A/B/' file > file-tmp -> mv file-tmp file what can i do to just let sed edit and save the "file" (4 Replies)
Discussion started by: fedora
4 Replies

7. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

8. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

9. Shell Programming and Scripting

sed / awk - inplace or inline edit

I need to remove the '&' from a file. In each line of the file, the fields are separated by ^K. I only want to remove '&' if it exists in field number 9. (example of field 9: abc&xyz) I need to do an in place/in line edit. So far I have accomplished the following: awk -F '^K' '{print... (6 Replies)
Discussion started by: hemangjani
6 Replies

10. UNIX for Dummies Questions & Answers

edit file using sed (not create another!)

Hi, I generally use Perl for this ex. perl -e 's/pattern/replace/g' -p -i <filename> I did something like this.. find . -type f -exec perl -e 's/pattern/replace/g' -p -i {} \; I want to do this with "sed" but what I get is the output being printed on the screen.. i can do sed... (3 Replies)
Discussion started by: oldtrash
3 Replies

Featured Tech Videos