| | |
line selection in a file and replace word
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
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
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
Reply urgently awaited!
Thanks for your patience.
Regards
Whizkidash
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)
#!/bin/bash echo "type file name to show" read fname cat $fname echo " choose the line in the file u need to replace word with read tname (suggest me from here so that i can achive my desired result)
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)
0
#4 22 Days Ago
Tell your professor uuoc 

bash Syntax (Toggle Plain Text)
#!/bin/bash get_file() { inp_file="" while ! test -f ${inp_file} || ! test "${inp_file}" do echo -n "File to read: [file.txt]: " read inp_file if [ "${inp_file}" = "" ]; then inp_file="./file.txt" fi done } get_line() { inp_line="" while ! [ ${inp_line} -eq ${inp_line} >> /dev/null 2>&1 ] || ! test ${inp_line} do echo -n "Line Number to replace: " read inp_line done } test_line_nbr() { local cnt=`wc -l $1 | awk '{ print $1 }'` if [ ${2} -le ${cnt} ] ; then valid_linenbr=1 else echo "" echo "Invalid line number" echo "" fi } get_search_word() { search_word="" while ! test ${search_word} do echo -n "Word to search for: " read search_word done } get_replace_word() { replace_word="" while ! test ${replace_word} do echo -n "Replacement value: " read replace_word done } valid_linenbr=0 get_file until [ ${valid_linenbr} -eq 1 ] do get_line test_line_nbr ${inp_file} ${inp_line} done get_search_word get_replace_word #use sed -i to replace the file in place #use sed "s//g" >> newfile to save the output sed "${inp_line} s/${search_word}/${replace_word}/g" ${inp_file}
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
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
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)
sk@sk:/tmp/d$ ls file.txt script.sh sk@sk:/tmp/d$ wc -l file.txt 11 file.txt sk@sk:/tmp/d$ ./script.sh File to read: [file.txt]: Line Number to replace: 999 Invalid line number Line Number to replace: 5 Word to search for: a Replacement value: xxxxxx a a a a xxxxxx a a a a a sk@sk:/tmp/d$
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
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
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)
BASH=/bin/bash BASH_ARGC=() BASH_ARGV=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu") BASH_VERSION='3.00.15(1)-release' COLORS=/etc/DIR_COLORS.xterm COLUMNS=80 DIRSTACK=() EUID=0 GROUPS=() G_BROKEN_FILENAMES=1 HISTFILE=/root/.bash_history HISTFILESIZE=1000 HISTSIZE=1000 HOME=/root HOSTNAME=LAB-A HOSTTYPE=i686 IFS=$' \t\n' INPUTRC=/etc/inputrc KDEDIR=/usr LANG=en_US.UTF-8 LESSOPEN='|/usr/bin/lesspipe.sh %s' LINES=24 LOGNAME=root 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:' MACHTYPE=i686-redhat-linux-gnu MAIL=/var/spool/mail/root MAILCHECK=60 OPTERR=1 OPTIND=1 OSTYPE=linux-gnu PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin PIPESTATUS=([0]="0") PPID=21897 PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"' PS1='[\u@\h \W]\$ ' PS2='> ' PS4='+ ' PWD=/root QTDIR=/usr/lib/qt-3.3 SHELL=/bin/bash SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor SHLVL=1 SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass SSH_CLIENT='::ffff:10.180.18.199 1109 22' SSH_CONNECTION='::ffff:10.180.18.199 1109 ::ffff:10.180.18.210 22' SSH_TTY=/dev/pts/1 SUPPORTED=en_US.UTF-8:en_US:en TERM=xterm UID=0 USER=root _=USERNAME mpi_selection= mpi_selector_dir=/var/lib/mpi-selector/data mpi_selector_homefile=/root/.mpi-selector 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)
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
Now when i run..
UREKA! it worked
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...
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..
Thanks for you patience!
Regards
whizkidash
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)
get_line() { inp_line="" # while ! [ ${inp_line} -eq ${inp_line} >> /dev/null 2>&1 || ! test ${inp_line} # do echo -n "Line Number to replace: " read inp_line #done }
Now when i run..
UREKA! it worked
[root@LAB-A Jobby]# ./particular1.sh File to read: [file.txt]: file1Line 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]: file1Line 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)
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]
[code]
...text here...
[/code]
![]() |
Similar Threads
- Can I replace a line in a text file or start looping from a line in ASP (ASP)
- How to extract a specific line in a text file (Java)
- Replace Line in .txt File (C++)
- Code Snippet: delete a line from a text file (C++)
- Replacing string in nth line (Shell Scripting)
- Extract Words from a line in a .txt file (C++)
- delete first line from a text file (C++)
- how to move to the second line in C++ .txt file reading? (C++)
- how do i read the last line of a text file? (Python)
Other Threads in the Shell Scripting Forum
- Previous Thread: Calling multiple scripts
- Next Thread: bash script (using find and grep)
| Thread Tools | Search this Thread |






