LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2011, 03:33 AM   #16
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12

can anyone help?
 
Old 03-31-2011, 08:00 AM   #17
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
To see if we are on the same page, you are wondering if you can remove certain parts and/or sections out of the file? You could use grep to find the pattern, and then use sed to take that pattern and remove it.
 
Old 03-31-2011, 08:12 AM   #18
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
yes, that's what i meant, but i don't use that anymore.

Now i need to check on filenames, to see if they contain some letters/words

for example, if i have a file named "testing.log", then i need to check if that file contains something like test, if that is the cause i need to do something (described in the script, but that doesn't matter). I need to figure out how to check on filenames and not the contect of a file
 
Old 03-31-2011, 08:24 AM   #19
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
i've got something like this:
Code:
var="0"
file=`find /some/foldere -type f -name "logfile.txt" -exec grep 06 {} \;`

if [ "$file" = "" ] ; then
var=0
else
var=1
fi
echo $var
output:
Code:
[root@host /]# ./check_test.sh
find: cannot open /some/folder/folder
0
the folder he cannot open is probably because i don't have enough rights to open it (i'm not admin or things like that)
so probably you can ignore the message.
but the 0 (zero) is that because he did found the file, or not?
 
Old 03-31-2011, 08:40 AM   #20
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Look at the code. The if statements say that if the file is equal to "nothing," to set var as 0. So in this case, no, nothing was found.

And actually according to the output, you ran it as root. Did you run that sample code exactly how it is? You need to specify what you want to find, and at the same time the file needs to exist.
 
Old 03-31-2011, 08:56 AM   #21
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
i'm just confused by everything..
it's really simpel to look in a file (with grep)

but why is it so hard just to check only the filename

let me explain again:
situation
/some/folder/test1.log
/some/folder/test_word.log
/some/folder/test_false.log
/some/folder/test_true.log

so i need to test the folder /some/folder if a certain file exists (let's say test_false.log).
So i really don't know how to fix that i tried a lot of possible solutions, but nothing seems to work...
i worked with find, grep, ... all sorts of combinations
it can't be that hard, can it??
 
Old 03-31-2011, 09:14 AM   #22
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
You could always do this:
Code:
find /some/folder/ | grep test_false.log
 
Old 03-31-2011, 09:20 AM   #23
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
i've got something like this:
Code:
file=`find /some/folder -type f -name "log*.txt" | cut -d / -f 3`
echo $file

if [ $file = log*.txt ]; then
	var="1"
	echo een 
else
	var="2"
	echo twee
fi
output:
Code:
logfile.txt
een
it works, but maybe there is an easier way (i'll try your suggestion, wait a moment)
 
Old 03-31-2011, 09:24 AM   #24
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
yes it works but it's kinda the same as i posted

so we've got 2 ways

probably i can use variables, like

Code:
file=`find /some/folder/ | grep $filename.txt | cut -d / -f 4`
i'm not sure i'm going to use it that way, but if i know that's possible, it would be nice to know
 
Old 03-31-2011, 10:31 AM   #25
toordog
Member
 
Registered: Jul 2010
Location: Montreal, Canada
Distribution: RedHat, Ubuntu, Solaris, AIX, BSD
Posts: 42

Rep: Reputation: 3
Quote:
Originally Posted by brownie_cookie View Post
yes it works but it's kinda the same as i posted

so we've got 2 ways

probably i can use variables, like

Code:
file=`find /some/folder/ | grep $filename.txt | cut -d / -f 4`
i'm not sure i'm going to use it that way, but if i know that's possible, it would be nice to know


Let put some more advanced topic. I suggest you to read about shell scripting (man ksh could help you a lot).

you could do something like this:

let assume that you would like to pass the pattern you seek in the file and the name of the file at launch

Code:
# Set filename to the first parameter after script name. Ex.: script.ksh filename($1) pattern($2)
typeset -x filename=$1  
typeset -x pattern=$2


for i in `find /some/folder -type f -name "filename"` #We are looking for all file that match filename (can be a pattern)
do
  newfilename=${i##*/} #This remove the path before the filename
  grep $pattern $i  # We need the path to grep the file for a pattern
  grep -v $pattern $i > /tmp/filename.tmp  #We create a new file without the line that match the pattern
  mv -f /tmp/filename.tmp $i  # We overwrite the original file with the one without the line that matched the pattern.
done


I put mostly example on how to do what you were looking to do. Like removing the path name how to remove line in a text file, I provided a loop in case you wanted to seek more than one file.


Regards
 
Old 03-31-2011, 02:57 PM   #26
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
To the OP - As you can see, there are many ways to do what you want to. Honestly, I always take the easy route, and if it works for me, then it works for me
 
Old 03-31-2011, 06:57 PM   #27
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
brownie_cookie

Hi There,

Another way to redirect file output of remote machine to local machine is via script.

Suppose you want to get the out of ls command you ran on the remote computer to your local machine then you can try the following steps:

1. On the remote machine create an script as follows:
vi /root/test

Contents of the test file
cd /root/
ls -l

2. chmod 755 test

3. On you local machine type the following command: ssh root@remotemachineIP/hostname "/root/test/" >> /localmachineroot/localmachinefile

Let me know if it works for you.

Last edited by T3RM1NVT0R; 03-31-2011 at 07:01 PM.
 
Old 03-31-2011, 07:15 PM   #28
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
@ brownie_cookie

Hi There,

As I can understand you are trying to find different files with the same name stored at different location (Correct me if I am wrong).

If that is the case then try running the following commands.

root# updatedb
root# locate filename.ext
 
Old 04-01-2011, 02:43 AM   #29
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Quote:
Hi There,

Another way to redirect file output of remote machine to local machine is via script.

Suppose you want to get the out of ls command you ran on the remote computer to your local machine then you can try the following steps:

1. On the remote machine create an script as follows:
vi /root/test

Contents of the test file
cd /root/
ls -l

2. chmod 755 test

3. On you local machine type the following command: ssh root@remotemachineIP/hostname "/root/test/" >> /localmachineroot/localmachinefile

Let me know if it works for you.
yes it works i've also found that solution yesterday, but thx

Quote:
To the OP - As you can see, there are many ways to do what you want to. Honestly, I always take the easy route, and if it works for me, then it works for me
true it's working now so i can put this thread as closed ^^

thanks again everyone for helping me out !!
 
Old 04-01-2011, 04:03 AM   #30
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by T3RM1NVT0R View Post
Hi There,

Another way to redirect file output of remote machine to local machine is via script.

Suppose you want to get the out of ls command you ran on the remote computer to your local machine then you can try the following steps:

1. On the remote machine create an script as follows:
vi /root/test

Contents of the test file
cd /root/
ls -l

2. chmod 755 test

3. On you local machine type the following command: ssh root@remotemachineIP/hostname "/root/test/" >> /localmachineroot/localmachinefile

Let me know if it works for you.
i've came up with a 'problem'. I know it works as you put the result of the ssh command in a file, but can you put it directly in a variable?

i'm currently using:
Code:
ssh root@remotemachineIP/hostname "/root/test/" >> $test
but when i echo it ($test) then i get no result

can someone help?


UPDATE
i've found my solution:
Code:
VAR=$(ssh user@host 'scriptname')

Last edited by brownie_cookie; 04-01-2011 at 04:44 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] iptables redirect : local to remote public host paziulek Linux - Networking 2 05-17-2010 03:34 PM
Redirect local DNS query to remote DNS server on non standard port? rock_ya_baby Linux - Server 8 04-13-2010 05:31 AM
redirect output to remote server via ssh packets Programming 4 05-19-2009 09:30 PM
Redirect Output to remote PC possible?? wilkija Linux - Newbie 4 10-03-2006 01:43 PM
Synaptic gui X redirect from remote to local station issue r00tnuke Red Hat 1 09-22-2003 03:06 PM


All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration