INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

BASH Scripting

BASH Scripting

(OP)
Hi,

I understand how to copy a specific line to a file, but is it possible to copy a specific line from file1.log and print it to line 2 within file2.log?
If so, how.

Thank you

RE: BASH Scripting

Do you mean insert to line 2, or overwrite line 2?

This isn't pretty, but it should work (doing insert)...

CODE

# Putting line 3 of file1.log to second line of file2.log

LINE=3

( head -1 file2.log
  head -${LINE} file1.log | tail -1
  tail --lines=+2 file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log 

RE: BASH Scripting

If you want to replace line 2 in file2.log...

CODE

# Putting line 3 of file1.log to second line of file2.log

INSERT=2
REPLACE=3
WHICH=${REPLACE}

LINE=3

( head -1 file2.log
  head -${LINE} file1.log | tail -1
  tail --lines=+${WHICH} file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log 


RE: BASH Scripting

(OP)
Thank you SamBones for the last 2 replies.
If you can, could you help me with the below:

I need to create a program in which it does the following:
- Select a word from file.log
- print it into /etc/network/services under wireless key
- ping the network
- - if successful save the successful ping to a file
- if not, then try the next word in the list

Huge Thank you

Thank you

RE: BASH Scripting

Uh, no, this isn't a programming service.

Why don't you try writing it yourself and then post the code and any error messages you get. Post questions about specific problems you are having. Then we can help you work through the problems.

Doing it that way will help you learn how to do it yourself much faster than taking code that you don't understand from some stranger on the Internet.

RE: BASH Scripting

(OP)
Ok

Would a for loop be best suited for this job?

Thank you

RE: BASH Scripting

(OP)
Hi SamBones

Another Question with the below:

CODE --> bash

#!/bin/bash
LINE=3
( head -1 file2.log
  head -${LINE} file1.log | tail -1
  echo "line"  >> -${LINE} file2.log
  tail --lines=+2 file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log 

If I wanted to echo a line next to the word, how would that be done, as I've failed with the above.

Thank you

RE: BASH Scripting

echo "line" >> file2.log

you may want to touch file2.log sometime before you append. Some versions of unix/Linux may not like it if you append to a non-existent file.

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


RE: BASH Scripting

(OP)
Hi johnherman,

Unfornualty thats not what im looking for, so I want to echo "line" to a specific line, e.g. line 2.

Thank you

RE: BASH Scripting

This is not possible if the file is open for read/write, as you will not know where the file pointer is positioned. If (when) the file is closed, you can use awk. for this example, n = second line, t = text to insert, "line" in this case. Code is untested.

awk -v "n=2" -v "t=line" '(NR==n) { print t } 1' /path/to/file

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close