Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It isn't very clear from the above what it is you need/want.
- You show one input example and one output example. The output example has less fields than the input example.
- You mention around 2 lines of a user information. What is that supposed to mean.
- This is also unclear (what do you mean by days): i have to fetch few days from this txt like username
- Excel files are, in general, comma separated and your example is space separated.
Please describe in detail what it is you want/need otherwise you'll get answer that will not help you.
Also include what you have already tried.
BTW: Do not mark your threads as being urgent. It might be for you, but we are all volunteers here at LQ and it isn't urgent for us and we don't (try to) solve threads based on urgency.
i have a userlog.txt file it has the rows like
username: test
email: test@test.com
emp ID: 11test
account: sbi
location: ban
sex: male
telep: 0000
....
etc
from this txt file i want to take particular informations like username, email, emp id, sex. out them to excel are csv formate as below.
From this i want to take only few like username, email id, and emp id if any information miss in data 0 should apply there.
What do you mean by missing?
In that list some user will not have the entry sex: in that case 0 should apply in excel sheet
- Is the complete entry missing? I.e.: sex: male is missing.
its complete
- Or is the value missing? I.e.: male is missing from the sex: entry.
Quote:
output should be in excel username has the name, email id has email ids, and emp id should have employe code
Are you sure you want the entries to be space separated and not comma separated (which is the standard way)?
yes its comma separated so that i can get the datas in individual colum.
output should look like this
test1 | test1@test | 01test | male
test2 | test2@test | 02test | 0 if the files is missing
output should be in excel username has the name, email id has email ids, and emp id should have employe code
Are you sure you want the entries to be space separated and not comma separated (which is the standard way)?
yes its comma separated so that i can get the datas in individual colum.
output should look like this
test1 | test1@test | 01test | male
test2 | test2@test | 02test | 0 if the files is missing
This doesn't make sense whatsoever!!
The output can look like this (comma separated):
Code:
test1,test1@test.com,test1,test1
OR space separated:
Code:
test1 test1@test.com test1 test1
OR pipe separated:
Code:
test1|test1@test.com|test1|test1
I also notice that:
- some entries do not have a space between the fields: country:IN Is this a typo or are these actually present?
- Some fields start with a capital: Department: it Are the fields always written the same way or is the following entry also possible: department: it
Answer the questions in detail otherwise we are not able to help you.
And I told you before to use [code] [/code] tags around the examples you post!
well try to put some delimiters between inputs then use variables in a script
username=$(cat file.txt | grep "username" | sed 's/.* //g)
this is just an example , you could put delimiters inside that file , grep | wc -l how many entries , then loop for entries in $(how many); get values from first to next entry , and grep there for variables like above.
then using csv u can echo >> towards another file echo $username","$email"," >> new_file.csv
make a folder and a shell script run.sh containing
Code:
#!/bin/bash
rm new.csv
rm tmp.file
## Begin header ##
echo "username","email","emp_ID" > new.csv
## End header
function search {
## Add here next variables , have to substitute values ###
username=$(cat tmp.file | grep "username" | sed 's/.* //g')
email=$(cat tmp.file | grep "email" | sed 's/.* //g')
emp_id=$(cat tmp.file | grep "emp ID" | sed 's/.* //g')
## Populate new file rows ##
echo $username" "$email" "$emp_id >> new.csv
}
lenght=$(cat file.txt |wc -l)
for line in `seq 1 $lenght`
do
if [ "$(sed ''$line'!d' file.txt)" != "" ] ; then echo $(sed ''$line'!d' file.txt) >> tmp.file ; else search ; echo "" > tmp.file
fi
echo $line
done
rm tmp.file
put that text file in same folder
make run.sh executable and run it
only username and email was added for example , you can add more identically.
this is what i got running my script
In this case , the delimiter itself is the gap ( empty rows ) between entries, gaps detected and used to restart search in the next block
Thank you so much brother but everything executed pls help me out and here i will give u one permission
pls give me script for this it will be more helpful
We will be very happy to HELP YOU with a script...but we are NOT going to write one for you. If you want someone to write programs for you, post this job in the LQ Job marketplace (along with how much you're willing to PAY to have your work done for you), and I'm sure you'll get it done quickly.
Otherwise, please spell out your words, and post what you've written/tried so far. You've been given MANY examples/samples thus far, and should be able to modify those examples to work for you.
well to use Microsoft's Excel
you NEED microsoft Office
and to run the current Microsoft Office , you NEED to be running Windows 7
or
win8 with office365
so
you need to use Microsoft's Visual Basic on a windows computer
or Perl,python on a windows computer
or
use Openoffice's "Calc"
but
using Microsoft's proprietary *.docx" format is NOT recomended
use openoffice format .sxc
as to "a script"
a python one would be able to easily convert the "text files" to a CSV file
then it is a very simple matter to import the csv file into OO's calc.
sorry but i just made ya a free gift. power is where knoledge is...therefore stop wasting your and other good ppl time. it is simply not fair!
made and tested that script for ya in 20 minutes. it simply works !
all u got to do was to add multiple rules as i explained into script.
shame on you !
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.