Replicate a field
Hello.
I have a file containing text. I want to replicate a specific field. For example, I might want to append a copy of the second word of each line to the end of that line. Have: Once upon a midnight dreary, while I pondered weak and weary, Over many a quaint and curious volume of forgotten lore, Want: Once upon a midnight dreary, while I pondered weak and weary, upon Over many a quaint and curious volume of forgotten lore, many Is there a Linux command which will do this? I seek a basic command, not awk, not Perl, because I haven't learned those things yet. Daniel B. Martin |
First, let's define some terms: There are no "Linux commands". There are shell commands---BASH being the most common shell---and there are a bazillion utilities, applications, etc.
For text manipulation, common utilities include SED, AWK, and Perl. (Maybe Python also) The BASH man pages will tell you about the commands built-in to BASH. Second, I do not recommend posting a question here, and then placing restrictions on what solutions are offered. In fact, since you are talking about fields, I suspect that AWK may be one of the better choices. I assume you want to do this on a line by line basis. Thus, you cannot simply use one tool to grab a word into a variable, and then make a second pass to add that variable to the end of the line. |
Indeed ... this one screams "awk" and "perl" at the top of
its lungs... Code:
awk '{print $0", "$2}' file Code:
... in the line which would throw "cut" off. Personally I find the awk version cleaner and more concise. Cheers, Tink |
Quote:
Code:
#!/bin/bash Code:
#!/bin/bash |
Quote:
This is the Newbie Forum. I am a newbie, learning Linux on my own. I can't learn all of it at once, so I'm starting with what I mistakenly called Linux commands. Commands such as sed and grep are so powerful that I want to develop competence and confidence with them before moving on to awk or Perl. If I place no bounds on solutions some members will produce awk or Perl solutions. Then they feel betrayed when I won't use their hard work. That's because I am unwilling to use code that I don't understand. Daniel B. Martin |
Quote:
The only thing I can offer is that the work required to apply the wrong tool often eclipses the work required to learn the right tool. I have personally demonstrated this by coming up with some totally convoluted SED code and then watching the AWK experts swoop in with something far better. I recommend learning all of the most common tools in the depth required to get your work done. In my case, I know SED and GREP well enough to know what problems will be difficult or even impossible. From this, I know when I need to dig back into AWK and learn a bit more. |
Code:
$ echo "this is test text" | sed -r 's:^([^ \t]+[ \t]+)([^ \t]+)(.*)$:\1\2\3, \2:' |
I love it!!!! Another SED fanatic is released into the world.
This eloquently demonstrates my point above: Quote:
|
I think we can do it a little different, but same result:
Code:
echo "this is test text" | sed -r 's:[ \t]+([^ \t]+).*:&, \1:' |
Yet another different approach...
Code:
paste -d' ' file <(cut -d' ' -f2 file) |
Quote:
There are many commands in the toolset, each with pros and cons for solving various problems. I doubt that any of us are totally fluent with them all. It is not necessary, even if possible, to completely master each before moving on to the next. Another approach is to learn simple usage of an increasing number and gradually extend that knowledge as convenient, as need arises. This problem suits awk particularly well, allowing Tinkster to offer the simple and comparatively comprehensible Code:
awk '{print $0", "$2}' file
|
Quote:
Some respondents misread the original post. The objective is to append the second word in each line to that line. There was no need for an additional comma. With that clarification, several of the offered code segments could be simplified. Daniel B. Martin |
Quote:
Technical Excellence may be defined as "completeness of function coupled with economy of means." Your solution qualifies as TE! For extra credit: show how the output may be directed to a file rather than standard output. Daniel B. Martin |
Quote:
Code:
ls > my_file Code:
command > my_file 2>&1 In case you want them in different files Code:
command > my_file.stdout 2> my_file.stderr |
> redirects stdout to a file.
2> redirects stderr to a file. &> redirects both stdout and stderr to a file. |
All times are GMT -5. The time now is 08:45 PM. |