How can I add a newline In a text file using Shell Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I add a newline In a text file using Shell Script
# 1  
Old 08-30-2011
How can I add a newline In a text file using Shell Script

Good day ...

Well i do have this project in school, in our Principles Of Operating System Class
We are using Cygwin....

And our project goes like this...

Create a dictionary using cygwin. Display the following menu at the start of
execution

1-add a word in the dictionary # specify the meaning
2-search a word # if word exists, show the meaning of the word
2-delete a word
4-delete all words in the dictionary

#NOTE: The program should ask the user if he wishes to continue or not.


And here is my Code....

ans=yes
while [ $ans = yes ]

do

echo 1-Add a word in the dictionary
echo 2-Search a word in the dictionary
echo 3-Delete a word in the dictionary
echo 4-Delete all words in the dictionary
echo -n "Choose which to execute: "
read ENTRY

case $ENTRY in

1)

echo -n "Enter a word: "
read word
echo $word " " | cat >> word.txt
echo -n "Enter the meaning: "
read meaning
echo $meaning | cat >> word.txt

;;

2)

SUCCESS=0

echo -n "Enter the word to search: "
read word

grep -q "$word" "word.txt"
if [ $? -eq $SUCCESS ]
then
awk "/$word/" word.txt
else
echo Word not found!
fi

;;

3)

#I still don't know what code to delete a word from the textfile
#If you can supply a good code to delete a word, that would be a big help

;;

4)

echo | cat > word.txt | echo Successfully Deleted!

;;

echo -n "Do you want to continue?yes or no: "
read ans

done




In the case 1 , I wanted that everytime I save a word & meaning to a notepad it

would appear like this in the notepad

#example

Clandestine means Secret
Loquacious means Talkative


but in my code, it appears like this in the notepad

#example

Clandestine means Secret Loquacious means Talkative




Does anyone know what is the code for my desired output in the notepad in case 1?

Can anybody help me with my code , especially in case 1 and case 3?

Your immediate response regarding this matter is so much appreciated



Thank you so much in advance ^_^ God bless ....
# 2  
Old 08-30-2011
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
This User Gave Thanks to Ygor For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

3. Shell Programming and Scripting

Shell to remove a newline char from selected rows in a file.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (3 Replies)
Discussion started by: mailme0205
3 Replies

4. Shell Programming and Scripting

How can I add a newline In a text file using Shell Script

Good day ... Well i do have this project in school, in our Principles Of Operating System Class We are using Cygwin.... And our project goes like this... Create a dictionary using cygwin. Display the following menu at the start of execution 1-add a word in the dictionary # specify the... (1 Reply)
Discussion started by: kpopfreakghecky
1 Replies

5. Shell Programming and Scripting

Newline in my script-built shell variable

Hi all, I'm not exactly a shell script guru, so this is probably a very silly question and I'm not seeing the point, but you know, sometimes it happens... I have this script which adds entries to local arp cache using it to find the corresponding IP address. # export MAC=00:25:90:34:3d:f2... (16 Replies)
Discussion started by: micantox
16 Replies

6. Shell Programming and Scripting

How to avoid Newline character in generated text file?

Hi All, Just need small help in resolving the special new line character in generated output file. In one of my shell script I am using following lines to get the spool file (i.e. sfile.txt) and AAA_XXXX_p111_n222.txt AAA_YYYY_p111_n222.txt Here assuming v_pnum="p111" v_nid="n222" ... (1 Reply)
Discussion started by: shekharjchandra
1 Replies

7. Shell Programming and Scripting

Help substituting text in a file having a single line but no newline char

Hello, Need help substituting a particular word in a file having a single line but no newline character at the end. I was trying to use sed but it doesn't work probably because there is no newline char at the end of the line. $ cat hlq_detail /outputs/alvan23/PDFs/bills $ cat... (5 Replies)
Discussion started by: Shan_u2005
5 Replies

8. Shell Programming and Scripting

Execute unix shell script to text file using the script

Hi all, I am beginner in UNIX...I want to use unix shell script to create text.file...I know how to use using by command...can anybody tell me for the script? Thanks i changed the threads title from "tex file" to "text file", because "tex" would probably be misunderstood as reference to... (4 Replies)
Discussion started by: mastercar
4 Replies

9. Shell Programming and Scripting

add newline in file after finding specific text

Hi All, I am tring to insert a newline with "/" in a text file whenever there is the text "end;" right now I have inside file: . . end; I want to have: . . end; / I tried doing the following within the file :g/^end;/s//end; \/ / (4 Replies)
Discussion started by: jxh461
4 Replies

10. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies
Login or Register to Ask a Question