line selection in a file and replace word

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster

line selection in a file and replace word

 
0
  #1
23 Days Ago
Hi Team,

Need your urgent help for the same
Well i have a file which consist of million records.

Question is :

Prepare a script which will cat that particular file..and then,the user will be asked to choose which line in the file he needs to modify the word and replace which the user had inputed.

The script should be interactive as the user will be inputting his values as per line selection made by him and replacing the word

Thanks for your patience!

regards
Whizkidash
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,230
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
23 Days Ago
So ... what is your question?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster
 
0
  #3
23 Days Ago
Hi Team & Snake

Question is :

file.txt
this is the record one
this is the record two
this is the record three
this is the record four
this is the record five

As you could see the above file there are five rows in five line.
Now, 1) i want a script which should be interactive which should first cat the file which the user has inputed the file to modify..
2) Then he should be able to replace the word as per line-wise
i.e: suppose he wants to change the word four, which is in line 4.. and so on 3) once done he should move the modifiyed file to a new file..


The above was just a typical example.. Actually it neads to be done on a file consist of 100 lines...

Please provide a universal script which can used to modify/replace any line wise file in my directory on my host box.


I have tried

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. echo "type file name to show"
  3. read fname
  4. cat $fname
  5. echo " choose the line in the file u need to replace word with
  6. read tname
  7. (suggest me from here so that i can achive my desired result)
  8.  

Reply urgently awaited!

Thanks for your patience.
Regards
Whizkidash
Last edited by peter_budo; 22 Days Ago at 7:50 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,230
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
22 Days Ago
Tell your professor uuoc

  1. #!/bin/bash
  2.  
  3. get_file()
  4. {
  5. inp_file=""
  6. while ! test -f ${inp_file} || ! test "${inp_file}"
  7. do
  8. echo -n "File to read: [file.txt]: "
  9. read inp_file
  10. if [ "${inp_file}" = "" ]; then
  11. inp_file="./file.txt"
  12. fi
  13. done
  14. }
  15.  
  16. get_line()
  17. {
  18. inp_line=""
  19. while ! [ ${inp_line} -eq ${inp_line} >> /dev/null 2>&1 ] || ! test ${inp_line}
  20. do
  21. echo -n "Line Number to replace: "
  22. read inp_line
  23. done
  24. }
  25.  
  26. test_line_nbr()
  27. {
  28. local cnt=`wc -l $1 | awk '{ print $1 }'`
  29. if [ ${2} -le ${cnt} ] ; then
  30. valid_linenbr=1
  31. else
  32. echo ""
  33. echo "Invalid line number"
  34. echo ""
  35. fi
  36. }
  37.  
  38. get_search_word()
  39. {
  40. search_word=""
  41. while ! test ${search_word}
  42. do
  43. echo -n "Word to search for: "
  44. read search_word
  45. done
  46. }
  47.  
  48. get_replace_word()
  49. {
  50. replace_word=""
  51. while ! test ${replace_word}
  52. do
  53. echo -n "Replacement value: "
  54. read replace_word
  55. done
  56. }
  57.  
  58. valid_linenbr=0
  59. get_file
  60.  
  61. until [ ${valid_linenbr} -eq 1 ]
  62. do
  63. get_line
  64. test_line_nbr ${inp_file} ${inp_line}
  65. done
  66.  
  67. get_search_word
  68. get_replace_word
  69.  
  70. #use sed -i to replace the file in place
  71. #use sed "s//g" >> newfile to save the output
  72. sed "${inp_line} s/${search_word}/${replace_word}/g" ${inp_file}
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster
 
0
  #5
22 Days Ago
Hello Snake,

When I run this script provided by you..
It give me the below error... as you could see i want to replace line number 2 which i have specified but still its not proceeding ahead with the script
File to read: [file.txt]: file1
Line Number to replace: 2
Line Number to replace: 2
Line Number to replace: 2
Line Number to replace: 2
Line Number to replace:

Please assist me with the same!

regards
Whizkidash
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,230
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #6
22 Days Ago
What version of bash are you running? The script is designed to make you re-enter the line number if you enter a non-numerical value, or, if you enter a line number longer than your file. Type set at the command line and post all of the shell-relevant environment variables back here.

Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:/tmp/d$ ls
  2. file.txt script.sh
  3. sk@sk:/tmp/d$ wc -l file.txt
  4. 11 file.txt
  5. sk@sk:/tmp/d$ ./script.sh
  6. File to read: [file.txt]:
  7. Line Number to replace: 999
  8.  
  9. Invalid line number
  10.  
  11. Line Number to replace: 5
  12. Word to search for: a
  13. Replacement value: xxxxxx
  14. a
  15. a
  16. a
  17. a
  18. xxxxxx
  19. a
  20. a
  21. a
  22. a
  23. a
  24.  
  25. sk@sk:/tmp/d$
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster
 
0
  #7
21 Days Ago
Hi scott,

Below is the details needed by you:

1) bash -version
GNU bash, version 3.00.15(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc




2)
[root@LAB-A ~]# set
Shell Scripting Syntax (Toggle Plain Text)
  1. BASH=/bin/bash
  2. BASH_ARGC=()
  3. BASH_ARGV=()
  4. BASH_LINENO=()
  5. BASH_SOURCE=()
  6. BASH_VERSINFO=([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
  7. BASH_VERSION='3.00.15(1)-release'
  8. COLORS=/etc/DIR_COLORS.xterm
  9. COLUMNS=80
  10. DIRSTACK=()
  11. EUID=0
  12. GROUPS=()
  13. G_BROKEN_FILENAMES=1
  14. HISTFILE=/root/.bash_history
  15. HISTFILESIZE=1000
  16. HISTSIZE=1000
  17. HOME=/root
  18. HOSTNAME=LAB-A
  19. HOSTTYPE=i686
  20. IFS=$' \t\n'
  21. INPUTRC=/etc/inputrc
  22. KDEDIR=/usr
  23. LANG=en_US.UTF-8
  24. LESSOPEN='|/usr/bin/lesspipe.sh %s'
  25. LINES=24
  26. LOGNAME=root
  27. LS_COLORS='no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:'
  28. MACHTYPE=i686-redhat-linux-gnu
  29. MAIL=/var/spool/mail/root
  30. MAILCHECK=60
  31. OPTERR=1
  32. OPTIND=1
  33. OSTYPE=linux-gnu
  34. PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
  35. PIPESTATUS=([0]="0")
  36. PPID=21897
  37. PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'
  38. PS1='[\u@\h \W]\$ '
  39. PS2='> '
  40. PS4='+ '
  41. PWD=/root
  42. QTDIR=/usr/lib/qt-3.3
  43. SHELL=/bin/bash
  44. SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
  45. SHLVL=1
  46. SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
  47. SSH_CLIENT='::ffff:10.180.18.199 1109 22'
  48. SSH_CONNECTION='::ffff:10.180.18.199 1109 ::ffff:10.180.18.210 22'
  49. SSH_TTY=/dev/pts/1
  50. SUPPORTED=en_US.UTF-8:en_US:en
  51. TERM=xterm
  52. UID=0
  53. USER=root
  54. _=USERNAME
  55. mpi_selection=
  56. mpi_selector_dir=/var/lib/mpi-selector/data
  57. mpi_selector_homefile=/root/.mpi-selector
  58. mpi_selector_sysfile=/etc/sysconfig/mpi-selector

3) I have been following the same as you could see below the file1 consist of 6 lines.
[root@LAB-A jobby]# wc -l file1
6 file1

Also when i execute the script with the line number to replace 2 (see below).It does not proceed ahead. Even though i key in the blank space the script tends to be the same..It does not show me any invalid statements...

[root@LAB-A jobby]# ./particular1.sh
File to read: [file.txt]: file1
Line Number to replace: 2
Line Number to replace:
Line Number to replace:
Line Number to replace:
Line Number to replace:
Line Number to replace:


Do let me know if you need still any inputs from me.
Thanks for your patience!

Regards
Whizkidash
Last edited by peter_budo; 20 Days Ago at 4:32 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 15
Reputation: whizkidash is an unknown quantity at this point 
Solved Threads: 0
whizkidash's Avatar
whizkidash whizkidash is offline Offline
Newbie Poster
 
0
  #8
21 Days Ago
Hi scott,
Did you find any thing on the set variables needed by you.
As per the script provided by you..
have hashed out the below contents

Shell Scripting Syntax (Toggle Plain Text)
  1. get_line()
  2.  
  3. {
  4. inp_line=""
  5. # while ! [ ${inp_line} -eq ${inp_line} >> /dev/null 2>&1 || ! test ${inp_line}
  6. # do
  7. echo -n "Line Number to replace: "
  8. read inp_line
  9. #done
  10. }

Now when i run..

UREKA! it worked

[root@LAB-A Jobby]# ./particular1.sh File to read: [file.txt]: file1
Line Number to replace: 2
Word to search for: this
Replacement value: oracle
hi this is jobby
oracle is me
this is w2
this is jobby's bay
this is next datacenter
this is next to jobby

But the mystery still remains unresolved..
Suppose if i want to replace "is" on line one it
then...

[root@LAB-A Jobby]# ./particular1.sh File to read: [file.txt]: file1
Line Number to replace: 1
Word to search for: is
Replacement value: oracle
hi thoracle oracle jobbythis is me
this is w2
this is jobby's bay
this is next datacenter
this is next to ashwin

Did you get it..it well i wanted is of third field to be replaced..

I guess awk does the trick..

awk 'NR==1{$3="oracle"}1' file1 can u suggest me using that in our script instead of using sed.


Thanks for you patience!
Regards
whizkidash
Last edited by peter_budo; 20 Days Ago at 4:33 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,230
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #9
20 Days Ago
I don't understand your question. Try to ask it again and please use code tags when posting code on daniweb. It converted half of your paste to emoticons and was very hard to read:

[code]
...text here...
[/code]
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC