944,193 Members | Top Members by Rank

Ad:
Nov 10th, 2009
0

line selection in a file and replace word

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 10th, 2009
0
Re: line selection in a file and replace word
So ... what is your question?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 11th, 2009
0
Re: line selection in a file and replace word
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; Nov 11th, 2009 at 7:50 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 11th, 2009
0
Re: line selection in a file and replace word
Tell your professor uuoc

bash Syntax (Toggle Plain Text)
  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}
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 12th, 2009
0
Re: line selection in a file and replace word
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 12th, 2009
0
Re: line selection in a file and replace word
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$
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 12th, 2009
0
Re: line selection in a file and replace word
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; Nov 13th, 2009 at 4:32 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 13th, 2009
0
Re: line selection in a file and replace word
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; Nov 13th, 2009 at 4:33 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 13th, 2009
0
Re: line selection in a file and replace word
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]
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Calling multiple scripts
Next Thread in Shell Scripting Forum Timeline: bash script (using find and grep)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC