Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Basically, its a game server (minecraft) and I have a situation where I am attempting to automate new world generation. Basically, I have a file that gets generated happily which has this snippet in it.

file1.txt

keepSpawnInMemory: 'true'
spawnLocation:
  ==: MVSpawnLocation
  yaw: 0.0
  pitch: 0.0
  z: 256.5
  y: 62.0
  x: -223.5
autoLoad: 'true'
bedRespawn: 'true'

This works fine, but I need to extract the z: 256.5, y:62.0 and x: -223.5 respectively and then write a file called file2.txt with the following lines.

 world: mine
 x: -223.5
 y: 62.0
 z: 256.5
 yaw: 0.0
 pitch: 0.0
 name: mine

I assume I'll be using something like awk to extract the initial numbers, assigning them to variables then writing the file? I have never used awk before, and I am insanely confused. Appreciate any help in this guys. Im using Ubuntu linux, bash scripts. Can I extract text like that and make them into variables using awk and bash?

EDIT:

I did forget to mention that the file 1 has multiple instances of x,y and z. It can be accessed here: http://pastebin.com/TqaLcWzV

Is there a way to start at a certain line? For example, z, y and x will always be on lines 183, 184 and 185 respectively.

share|improve this question
    
Do you need only x, y, z lines from file1? –  cuonglm Aug 3 at 12:56
    
Yeah, yaw and pitch will always be 0, so they dont really need to be allocated to a variable. Apologies also, the example file 2 should have the same numbers as the first file for the x,y and z. –  user2809413 Aug 3 at 12:58

2 Answers 2

up vote 1 down vote accepted

Try this script:

#!/bin/bash

FILE1=/path/to/file1
FILE2=/path/to/file2

#extracting lines 183 to 185
FILE1=$(awk 'FNR>=183&&FNR<=185' $FILE1)

#extracting variables    
Z=$(echo "$FILE1" | grep z: | awk '{print $2}')
Y=$(echo "$FILE1" | grep y: | awk '{print $2}')
X=$(echo "$FILE1" | grep x: | awk '{print $2}')
#YAW=$(echo "$FILE1" | grep yaw: | awk '{print $2}')
#PITCH=$(echo "$FILE1" | grep pitch: | awk '{print $2}')

cat <<EOF > $FILE2
 world: mine
 x: $X
 y: $Y
 z: $Z
 yaw: 0.0
 pitch: 0.0
 name: mine
EOF

If you want it to also extract the YAW and PITCH:

  • Change the awk line to extract from line 181:

    FILE1=$(awk 'FNR>=181&&FNR<=185' $FILE1)
    
  • Uncomment the YAW and PITCH lines

  • Change the yaw: and pitch: at the very bottom to be:

    yaw: $YAW
    pitch: $PITCH
    
share|improve this answer
    
Thats perfect! Thanks heaps!! –  user2809413 Aug 3 at 22:19

Do you really need to assign the values to variables? Since the variable: value pairs appear identically in both the input and output, you could just extract them textually, e.g.

$ awk '/mine:/,/allowFlight/ {if ($1 ~ /^[xyz]:/) print;}' file1.txt
      z: 256.5
      y: 62.0
      x: -223.5

You could put that directly in a here-document using command substitution I think:

cat > file2.txt << EOF 
 world: mine
$(awk '/mine:/,/allowFlight/ {if ($1 ~ /^[xyz]:/) print " "$1,$2;}' file1.txt)
 yaw: 0.0
 pitch: 0.0
 name: mine
EOF

(I prettified the output a little by using print " "$1,$2 instead of a simple print).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.