Reading value from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Reading value from a file
# 1  
Old 04-23-2019
Reading value from a file

Hi,
I have a file. The content is :
Code:
server1 database1 user1 profiletarget1 
server1 database1 user2 profiletarget2 
server1 database2 user1 profiletarget3

I want to have/extract from it:
Code:
sid=database1
 user=user1  profile= profiletarget1 
  do something
  user= user2  profile= profiletarget2
   do something
sid=database2 

user=user1     profile =profiletarget3
  do something

I tried some grep and awk combined without success.
Thank you.


Moderator's Comments:
Mod Comment
Please wrap all code, data, files, input & output/errors in CODE tags, like this:-
Quote:
[CODE]This is my code[/CODE]
It make posts far easier to read and it respects respects multiple space for indenting and fixed-width data

Last edited by rbatte1; 04-23-2019 at 10:31 AM..
# 2  
Old 04-23-2019
Hello big123456,

Not sure what do you mean by do something in your question, for printing you could try following.

Code:
awk '{print "sid="$2 ORS "user=",$3 OFS "profile=",$4 ORS "do something....."}'  Input_file

Thanks,
R. Singh
# 3  
Old 04-23-2019
Thank you.
"do something" would be a shell command to process those variables: sid, user, profile.
Regards.
# 4  
Old 04-23-2019
Hello big123456,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.




Kind regards,
Robin
# 5  
Old 04-23-2019
Hi,
Thank you.
I have run
Code:
for member in $(cat myfile.txt|grep server1|awk -F: '{print "sid="$2,"user="$3,"profile="$4}')
do
echo sid:$sid' user: '$user' profile:  '$profile
done

OS: 2018 x86_64 x86_64 x86_64 GNU/Linux
Regards.

Last edited by vgersh99; 04-23-2019 at 12:28 PM.. Reason: please start using code tags!
# 6  
Old 04-23-2019
not sure if your file is : separated or not - your sample file from post#1 seems to indicate that it's space separated, but..

Code:
#while IFS=: read server db user prof junk
while read server db user prof junk
do
  if [ "${server}" = 'server1' ]; then
     echo "server:[${server}] db:[${db}] user:[${user}] prof:[{${prof}]"
  fi
done < myInputFile

If the input file is : separated, uncomment the first #while comment-ed line and comment out the 2nd.
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Shell Programming and Scripting

Reading UNIX commands from file and redirecting output to a file

Hi All I have written the following script: #!/bin/ksh while read cmdline do echo `$cmdline` pid="$cmdline" done<commands.txt =========== commands.txt contains: ps -ef | grep abc | grep xyz |awk '{print $2}; My objective is to store the o/p of the command in a variable and do... (8 Replies)
Discussion started by: rahulparo
8 Replies

5. Shell Programming and Scripting

fatal: cannot open file `TNAME' for reading (No such file or directory)

Hi, I am running this command through a shell script and getting the error mentioned in the subject line: testing.awk -f x.txt TNAME My testing.awk file contains something like ++++++++++++++++++ #!/usr/bin/awk -f BEGIN{ TAB_NAME="INSERT_ONE_" ARGV ; } if ( $1=="JAM_ONE" &&... (1 Reply)
Discussion started by: kunwar
1 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. UNIX for Dummies Questions & Answers

Reading from a file(passing the file as input parameter)

hi I have a shell script say primary.sh . There is a file called params my scenario is primary.sh should read all the values and echo it for example i should pass like $primary.sh params output would be Abc ... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question