Insert text into file depending on variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text into file depending on variable
# 1  
Old 05-12-2009
Insert text into file depending on variable

Hey guys ,

i have a variable with the contents ...

NUMBER=4

and a test file with the contents

1248
1213
1214
1278
1200
3045
3444
2130

I want to execute a script that will produce the following output ( based on NUMBER=4) to be ...

create 1248
add 1213
add 1214
add 1278
create 1200
add 3045
add 3444
add 2130

If the variable , NUMBER =2 , the output should look like ...

create 1248
add 1213
create 1214
add 1278
create 1200
add 3045
create 3444
add 2130

if the variable Number=8,, the output should look like

create 1248
add 1213
add 1214
add 1278
add 1200
add 3045
add 3444
add 2130

The number of lines in the file is variable. For example , the total wc could be 9 , and the variable =3
Was looking at using ksh , sed , awk
I have reviewed the current listings, but can't see any examples similiar to this request.

Any ideas , suggestions , greatly welcome.
# 2  
Old 05-13-2009
Some more effort on your side wouldn't hurt ..., this seems like a homework.

Anyway ...

Code:
awk '{print (NR==1) ? "create " $0 : !(++i%n) ? "create " $0 : "add " $0}' n=4 file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash: Insert in a variable a file

hi all i have a problem in the bash shell. i'd like insert in a variable a file for example : i have a file datafine.log in this file there is : 17/JUN/2019 i want to insert the value of datafine.log in a variable. Regards Frncesco edit by bakunin: please use CODE-tags for your data... (2 Replies)
Discussion started by: Francesco_IT
2 Replies

2. Shell Programming and Scripting

Checking the file depending on the input variable

Hi I have a requirement of taking time as input variable outside the script.depending on the time it will check the file output .like ./script.sh <30 min> so script.sh should run every 5 minutes ie.6 times to check the output file.Can any one please help here. (7 Replies)
Discussion started by: netdbaind
7 Replies

3. Shell Programming and Scripting

Insert value to db from text file

Hi, I have a single value in insertval file. I want to load that value to database with the current date. I tried the below code but it is inserting <NULL> to database and echo $c is also null. cat insertval | awk -F ' ' '{print $1}' > c echo c=$c data=`sqlplus -s user/pwd@hostname <<EOF ... (5 Replies)
Discussion started by: Neethu
5 Replies

4. Shell Programming and Scripting

Redirect output to a different text file depending source of data

I have a list of DNS servers I need to look up information on. Each of these servers has a master and a slave database. Essentially what I need to do is create two text files for each server. One with the Master view and one with the Slave view. There's 20 servers, in the end I should have 40 text... (4 Replies)
Discussion started by: spartan22
4 Replies

5. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

6. Shell Programming and Scripting

insert line into file using variable

Hi all, I am trying to insert couple of lines before first occurance of line3 which occuring after line 5. So I identified the line 5 line number in the file. Also redirected the all line3 line number to out.txt. Now I have problem in inserting the line using the variable $rep. Please help me... (2 Replies)
Discussion started by: aimmanuel
2 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

insert a variable in the last column of a file

i want to insert a variable in the last column of a file, the columns are separated by "|". I want to insert the variable in everyline of the file . (7 Replies)
Discussion started by: dineshr85
7 Replies

10. Shell Programming and Scripting

how to insert text using variable

Hi i have a dilemma I need to write a script that takes two arguments. The first being a line of text, the second being a newly created file. The script should take the first argument and insert it into the very top ( the first line) of the file named in your second argument. Note! The... (1 Reply)
Discussion started by: mopimp
1 Replies
Login or Register to Ask a Question